![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| Welcome
to Tech Support Forum home to more then 136,000 problems solved. Issues
have included: Spyware, Malware, Virus Issues, Windows, Microsoft,
Linux, Networking, Security, Hardware, and Gaming Getting your
problem solved is as easy as: 1. Registering for a free account 2. Asking your question 3. Receiving an answer Registered members: * See fewer ads. * And much more..
|
| Want to know how to post a question? click here | Having problems with spyware and pop-ups? First Steps |
|
|||||||
| Web Design & Programming Discussion of web design, and server-side & client-side scripting |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Design Team Member
|
I noticed a different tutorial about uploading files to the server but I wanted to do one with more security stuff and with a different method. I personally like uploading files to a database better as it makes file management easier (delete, move, better control of who can download it, and you can store more information along with the file, like WHO uploaded it) plus people cannot execute files in the database as they can on your server (at least I have yet to hear of a case like this). I personally use MySQL, but there shouldn't be much of a difference between it and other databases. If you already have experience with PHP/MySQL, this should be real painless and suprisingly easy! If you don't, it's still much easier to grasp than uploading to a server (the 'tmp' folder stuff tends to throw people).
I'm going to do this tutorial as simply as I can - but you can add as much as you want...I'll talk a little bit about things you can do when I get to those parts! NOTE: At the end of this tutorial is the resource documents if you want to just upload the completed documents to your server and go from there (that's how I do it too!). We're going to use this to upload PICTURES. You should have... A) A server with PHP enabled B) A MySQL database C) Your connection settings (to connect to the database) First off, let's prepare a connection file that we can include in ever page that needs to connect to the database. 1) CREATE THE CONNECTION SETTINGS FILE We make an external PHP file and then include it so we don't have to paste the same thing in multiple pages or edit a bunch of pages if our connection settings change. This is a BIG deal if you have a huge site and suddenly have to change your password or something. connect.php PHP Code:
A) fid - file id, set at auto-increment so the database will automatically know to go to the next ID number. I typically name my ID's with whatever is being added to the table...like for users, I use 'uid', etc. B) name - whatever the file is named. C) type - MIME type. D) content - the actual file content. You can add other fields if you want to tag more information onto the file record. On one of my sites, I have fields to track the category it's listed under, the person that uploaded it, and the date it was uploaded. To keep it simple, we're just going to do the basic stuff. You might be a little confused asto how the file is physically stored to a database. What happens is the file's content is put into the content field through the BLOB (Binary Large Object) type. It's a part of the database now. The BLOB type we're going to use is MEDIUMBLOB which will allow up to 16 MB files. If you're thinking "HOLY COW! I don't want people to be uploading 16 MB files!" stress not my friend. We're going to throttle the file size and type later. You can add this table in two ways... A) a MySQL GUI (aka phpMyAdmin): Go into phpMyAdmin > your database > click the "SQL" tab > paste this: Code:
CREATE TABLE `files` ( `fid` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'unique id', `name` VARCHAR( 30 ) NOT NULL COMMENT 'file name', `type` VARCHAR( 30 ) NOT NULL COMMENT 'MIME type', `size` INT( 11 ) NOT NULL COMMENT 'file size', `content` MEDIUMBLOB NOT NULL COMMENT 'actual file' ) ENGINE = MYISAM COMMENT = 'Uploaded files' addtable.php PHP Code:
Great, now we have a table setup and ready for our files! Now it's time to build our form. 3) CREATE UPLOAD FORM Easy stuff! form.html HTML Code:
Upload your file to the database... <form action="upload.php" method="post" enctype="multipart/form-data" name="uploadform"> <input type="hidden" name="MAX_FILE_SIZE" value="350000"> <input name="picture" type="file" id="picture" size="50"> <input name="upload" type="submit" id="upload" value="Upload Picture!"> </form> 4) CREATE UPLOAD SCRIPT Alright, roll up your sleeves...and let's get down to the nitty gritty! A) First we need to make sure this file is to our requirements and kill it if it is not! Since it's a picture, we'll be checking the file size, type, and parameters. I'll try to note everything clearly within the code. PHP Code:
B) Now we extract the data from the file. PHP Code:
C) Store into the database. PHP Code:
1) Simply tell the user it is uploaded. PHP Code:
PHP Code:
PHP Code:
5) CREATE PAGE TO GET THE PICTURE We're not QUITE done yet though! We have to build a page that extracts the data from the database, otherwise it's just stored there and we can't do anything with it...much less display our image. PHP Code:
HTML Code:
<img src="getpicture.php?fid=1">
THAT'S IT! You can now upload pictures (and with some modification to this code, upload other files too) to your database and view them! ![]() RESOURCES: If you just skipped to the bottom and just want to install this on your server and go from there, here are the source documents... connect.txt - save as connect.php addtable.txt - save as addtable.php form.txt - save as form.html upload.txt - save as upload.php - NOTE: I used "wrap up" #3 above getpicture.txt - save as getpicture.php |
|
|
| Important Information |
|
Join the #1 Tech Support Forum Today - It's Totally Free!
TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free. Join TechSupportforum.com Today - Click Here |
|
|
#2 (permalink) |
|
Design Team Member
Join Date: Jul 2007
Location: Coventry, UK
Posts: 1,854
OS: Vista, various linux distros
|
Re: TUTORIAL: Upload Files To Database
Hey Redcore, just thought i'd show my appreciation, i like the way this works, very nice tutorial aswell.
Cheers, Jamey |
|
|
![]() |
| Thread Tools | |
|
|