CodySnider.com
| Main Menu | |||||||
|---|---|---|---|---|---|---|---|
|
| Guides | ||||
|---|---|---|---|---|
|
| Myspace Tools | ||
|---|---|---|
|
| Latest Articles |
|---|
| Statistics |
|---|
|
Members: 89 Articles: 130 Visitors: 175579 |
| Login |
|---|
| PHP File Upload from HTML Forms |
|
|
|
|
One of the essentials to every backend operation and many frontend applications, uploading from a form is a tool you'll want to have readily available and it's not that difficult to do.
HTML Code:<form enctype="multipart/form-data" action="uploader.php" method="POST"> Here is a brief description of the important parts of the above code:
Save that form code into a file and call it upload.html. If you view it in a browser it should look like this: Display: Choose a file to upload: 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. PHP - File Upload: What's the PHP Going to 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. PHP - File Upload: uploader.phpWhen 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. PHP Code:// Where the file is going to be placed 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. 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. PHP - File Upload: move_uploaded_file FunctionNow 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!). PHP Code:$target_path = "uploads/"; 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. If there was a problem then the error message "There was an error uploading the file, please try again!" would be displayed. PHP - File Upload: Safe Practices!Note: This script is for education purposes only. We do not recommend placing this on a web page viewable to the public. These few lines of code we have given you will allow anyone to upload data to your server. Because of this, we recommend that you do not have such a simple file uploader available to the general public. Otherwise, you might find that your server is filled with junk or that your server's security has been compromised. We hope you enjoyed learning about how to work with uploading files with PHP. In the near future we will be adding an advanced lesson that will include more security and additional features! |


