JDT |
FREE Downloads |
|
Creating a Simple File Uploader Using PHP - Part 3 |
||||
|
In this series of tutorials we will look at how to develop a simple file uploader using PHP. All of the code is freely available for download (License: GNU GPL) from this link: Download File Uploader. In this tutorial, we will look at how to create a form handler to process the uploaded file. In Creating a Simple File Uploader Using PHP - Part 2 we looked at how to produce a very simple form to enable someone to browse for, and select, a file to upload. In this tutorial we will look at how to store the selected file in a directory on a server. To do this, we need to use a form handler - we will call this "fileUpload.php". Getting the form dataThe first task that the form handler needs to perform is to get the data that was entered on the form. To do this, we'll use the following bit of PHP code:
$host=$_REQUEST['host']; The first three bits of data will be used to access and change the permissions on the directory where the file will be stored. We will look at how to do this in Part 4. Debugging the uploaded fileNext, the form handler contains a bit of debugging code:
// Debugging for uploaded file These lines will generate some messages on the screen when you upload a file. A successful upload will generate a set of messages similar to those shown below (sample data is included).
File name = redbox.gif Once you are happy that the uploader works fine, you can comment out these lines from the script. Here is a list of possible error codes:
Value: 0 - There is no error, the file uploaded with success. Check that the file is validThe form handler next checks that a valid file type has been uploaded. This is to prevent someone from trying to upload a script or something like that. This would be useful if you adapt the script to allow other people to upload files. We'll discuss this further in Part 5.
// Check that user has uploaded a valid file type If a valid file type has been uploaded, a test is performed to make sure that the file is not too big:
// Check that the file is not too big Process the uploaded fileIf there are no problems with the file, it can be stored on the web server in the location defined by '$targetfolder'.
// Open file, read into memory. rb means binary read mode. <- Go to Part 2 Go to Part 4 -> Go back to PHP Tutorials home page Go back to Tutorials home page
|
|