A very useful aspect of PHP is its ability to manage file uploads to your server. Allowing users to upload a file to your server opens a whole can of worms, so please be careful when enabling file uploads.
HTML Form
Before you can use PHP to manage your uploads, you must first build an HTML form that lets users select a file to upload.
HTML Code:<form enctype="multipart/form-data" action="uploader.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" value="Upload File" /> </form>After the user clicks submit, the data will be posted to the server and the user will be redirected to uploader.php. This PHP file is going to process the form data and do all the work.
- enctype="multipart/form-data" - Necessary for our to-be-created PHP file to function properly.
- action="uploader.php" - The name of our PHP page that will be created, shortly.
- method="POST" - Informs the browser that we want to send information to the server using POST.
- input type="hidden" name="MAX_FILE_SIZE" value="100000" - Sets the maximum allowable file size, in bytes, that can be uploaded. This safety mechanism is easily bypassed and we will show a solid backup solution in PHP. We have set the max file size to 100KB in this example.
- input name="uploadedfile" - uploadedfile is how we will access the file in our PHP script.
What PHP will do
Now that we have the right HTML form we can begin to code the PHP script that is going to handle our uploads. Typically, the PHP file should make a key decision with all uploads: keep the file or throw it away. A file might be thrown away from many reasons, including:This example is very simple and omits the code that would add such functionality.
- The file is too large and you do not want to have it on your server.
- You wanted the person to upload a picture and they uploaded something else, like an executable file (.exe).
- There were problems uploading the file and so you can't keep it.
PHP File uploader.php
When the uploader.php file is executed, the uploaded file exists in a temporary storage area on the server. If the file is not moved to a different location it will be destroyed! To save our precious file we are going to need to make use of the $_FILES associative array.
The $_FILES array is where PHP stores all the information about files. There are two elements of this array that we will need to understand for this example.Now we can finally start to write a basic PHP upload manager script! Here is how we would get the temporary file name, choose a permanent name, and choose a place to store the file.
- uploadedfile - uploadedfile is the reference we assigned in our HTML form. We will need this to tell the $_FILES array which file we want to play around with.
- $_FILES['uploadedfile']['name'] - name contains the original path of the user uploaded file.
- $_FILES['uploadedfile']['tmp_name'] - tmp_name contains the path to the temporary file that resides on the server. The file should exist on the server in a temporary directory with a temporary name.
NOTE: You will need to create a new directory in the directory where uploader.php resides, called "uploads", as we are going to be saving files there.PHP Code:// Where the file is going to be placed
$target_path = "uploads/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
We now have all we need to successfully save our file to the server. $target_path contains the path where we want to save our file to.
move_uploaded_file Function
Now all we have to do is call the move_uploaded_file function and let PHP do its magic. The move_uploaded_file function needs to know 1) The path of the temporary file (check!) 2) The path where it is to be moved to (check!).
If the upload is successful, then you will see the text "The file filename has been uploaded". This is because move_uploaded_file returns true if the file was moved, and false if it had a problem.PHP Code:$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
If there was a problem then the error message "There was an error uploading the file, please try again!" would be displayed.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks