Home > Articles > Security > Network Security

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend


Both of these methods work well when you only need to upload one file, but things start to get difficult when you want to upload more than one file.

PHP provides some neat tools for tackling the problem of uploading multiple files. The script in Listing 4 uses for statements in conjunction with variable variables to upload multiple files. The script has also been written in such a way so that scaling from 1 to 100 files is easy.

Create the script in Listing 4 (without the line numbers) in a text editor and name it upload2.php. This script allows you to upload a file from your local file system.

See the "How the Script Works" section after the script for line-by-line details on the script.

Listing 4: upload3.php

1. <html>
2. <head>
3. <title>File Upload Script 1</title>
4. </head>
5. <body>
6. <?
7. $uploads = 5;
8. if(isset($submit)) {
9. for($i = 0; $i < $uploads; $i++) { 
10. $uploaddir = "/full/path/to/your/upload/directory/";
11. $newfile = $uploaddir . ${"filename".$i};
12. if(copy(${"file".$i}, $newfile)) {
13. print("<h3>UPLOAD $i+1 SUCCESSFUL!</h3>");
14. } else {
15. print("<h3>ERROR UPLOADING FILE $i+1!</h3>");
16. }
17. }
18. } else {
9. ?>
20. <form action="upload3.php" method="POST"
[ccc] enctype="multipart/form-data">
21. <input type="hidden" name="MAX_FILE_SIZE" value="2097152">
22. <? for($i = 0; $i < $uploads; $i++) { ?>
23. <p>File:<input type="file" name="file<? echo $i ?>">
24. <br>Name of file on server: 
[ccc] <input type="text" name="filename<? echo $i ?>">
25. <?
26. }
27. ?>
28. <br><input type="submit" name="submit" value="UPLOAD">
29. </form>
30. <?
31. } 
32. ?>
33. </body>
34. </html>

How the Script Works

This description gives a line-by-line detail of Listing 4.


These lines are the normal HTML used to start the script.


This line starts the PHP portion of the script.


This line sets the $uploads variable. By changing this setting, you can change how many files are uploaded to the server. The default is five files. If you wanted to upload only three files, you would change this setting to 3.


Here, the script checks whether the Submit button has been pushed. If it has, then it executes the code starting on line 9. Otherwise, the script continues executing from line 18.


This line is the beginning of the for loop that will loop through lines 10–17.


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.


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 you are uploading.


Notice that the ${"filename".$i} variable looks rather odd. This is because it is a variable variable. If $i were 1, then this line would translate to the variable $filename1. The "dot" operator (.) tells the script to concatenate the string "filename" with the contents of the variable $i. The ${ } notation surrounding this mess tells the script to treat the newly created string as a variable. Hence, if $i = 1, then ${"filename".$i} = $filename1.


Line 10 attempts to copy the uploaded file, to the filename and path that is set in the $newfile variable. If the copy is successful, then line 13 is executed. If the file is not successful, then lines 14–17 are executed.


This line simply notifies the user that the current upload was successful. It prints out $i + 1 (instead of just $i) because $i starts at 0, instead of 1.


These lines are executed if the upload was not successful.


This line ends the for loop that was started on line 9.


These lines (through to the end of the script) are executed if the $submit variable has not been set.


These lines are the form in which the user enters his name.


These lines close out the if statement that began in line 7.


These lines end the script.

As you can see, these scripts make it easy for users (or even you) to upload files to a server. They don't need to fuss with an FTP client, and you can easily set up the script to organize uploads.

  • + Share This
  • 🔖 Save To Your Account