Uploading Files with PHP
Yet another one of the features that makes PHP such an easy language to use is its file functions. The file functions allow you to create nice utility such as a file-uploading script.
Before you begin, there are a couple things that you need to do. First, you need to make sure that the directory on the Web server to which you will upload the files has "world" write permissions. The easiest way to do this is to telnet to the server and execute the command:
chmod 777 /full/path/to/your/upload/directory
You will not be able to upload any files if you do not set the permissions correctly.
Secondly, you cannot upload files bigger than the upload_max_filesize variable set in the php.ini file. The default file size is 2,097,152 bytes, which is roughly 2MB.
Consult the PHP documentation to find out how to change this setting. You can find the current setting for your server by loading a simple script (such as info.php) with the phpinfo() function in it (see Listing 1).
Listing 1: info.php
<? phpinfo(); ?>
This article presents two different methods of uploading files to your Web server. The first method allows you to upload files from your local machine using the standard "browse for file" convention provided by the HTML <input type="file"> form element.
The second method allows you to upload files to your Web server that exist elsewhere on the Internet. You just need to provide a standard URL for the file, and the script automatically uploads it to the specified area of your Web server.
upload1.php
Create the script in Listing 2 (without the line numbers) in a text editor and name it upload1.php. This script allows you to upload a file from your local file system.
See the section "How the Script Works" after the script for line-by-line details.
Listing 2: upload1.php
1. <html> 2. <head> 3. <title>File Upload Script 1</title> 4. </head> 5. <body> 6. <? 7. if(isset($submit)) { 8. $uploaddir = "/full/path/to/your/upload/directory/"; 9. $newfile = $uploaddir . $filename; 10. if(copy($file, $newfile)) { 11. print("<h3> UPLOAD SUCCESSFUL!</h3>"); 12. } else { 13. print("<h3>ERROR UPLOADING FILE!</h3>"); 14. } else { 15. ?> 16. <form action="upload1.php" method="POST" [ccc] enctype="multipart/form-data"> 17. <input type="hidden" name="MAX_FILE_SIZE" value=" 2097152"> 18. <br>File:<input type="file" name="file"> 19. <br>Name of file on server: <input type="text" name="filename"> 20. <br><input type="submit" name="submit" value="UPLOAD"> 21. </form> 22. <? 23. } 24. ?> 25. </body> 26. </html>
How the Script Works
Here's a description of each line in Listing 2:
15 |
These lines are the normal HTML used to start the script. |
6 |
This line starts the PHP portion of the script. |
7 |
Here, the script checks if the Submit button has been pushed. If it has, then it executes the code starting on line 8. Otherwise, the script continues executing from line 14. |
8 |
This line sets the $uploaddir variable. This variable should be set to the full path of the directory where you want to upload your files. |
9 |
This line sets the $newfile variable. The $newfile variable is a combination of the $uploaddir variable and the $filename variable that is input into the form. This creates a variable to the full path and filename of the file that you are uploading. |
10 |
Line 10 attempts to copy the uploaded file, $file, to the filename and path that is set in the $newfile variable. If the copy is successful, then line 11 is executed. If the file is not successful, then line 13 is executed. |
11 |
This line is executed if the if statement in line 10 is true. |
1213 |
These lines are executed if the if statement in line 10 is false. |
1421 |
These lines are executed if the $submit variable has not been set. The $submit variable is set when the user pushes the Submit button. |
2224 |
These lines end the if statement started in line 7. |
2526 |
These final lines end the script. |