PHP script to upload multiple files

Source: Internet
Author: User
Tags copy file upload file upload script html form php code php script variable mysql database
Script | Upload Translator Note: This article is formerly known as "Creating a multi-file Upload script in PHP." I think this article is generally written, the code is not very professional, but it is relatively short, and a time can not find a better article, the translation of this. In fact, the PHP manual also has a section is to say that multiple file upload, we can contrast. Some words in the article are not very pleasing to the eye, so in accordance with the original intention to make a certain amount of modification. I have a limited level, but also hope that you master more guidance.

Introduction

As a PHP programmer, I've met a client who needs a form that can upload multiple files at the same time. So one night I took one hours to find the best and easiest way to do this. In this tutorial, the "for" loop statement will be the core code.

Script 1: Let the user set the number of files to upload

uploadform1.php

<title> File Upload </title>
<body>
<form name= "Form1" method= "Post" action= "uploadform2.php" >
<p> Please enter the number of files you want to upload at the same time, the maximum value is 9</p>
<p>
<input name= "Uploadneed" type= "text" id= "Uploadneed" maxlength= "1" >
</p>
<p>
<input type= "Submit" name= "submit" value= "Submit" >
</p>
</form>
</body>


The HTML code above is very simple. In this code, I set the MaxLength property of Uploadneed This component to 1, so the maximum value that the user can enter is 9. Of course, you can modify this part as you need to.

Script 2: Create a dynamic form

Well, after doing the following page, we'll finish half of the work. We use the FOR Loop statement to accomplish this task.

uploadform2.php
<title> File Upload </title>
<body>
<form name= "Form1" enctype= "Multipart/form-data" method= "post" action= "processfiles.php" >
<?php
Create a dynamic form
$uploadNeed = $_post[' uploadneed '];
for ($x =0; $x < $uploadNeed; $x + +) {
?>
<input name= "uploadfile<?php echo $x;? > "type=" File "id=" uploadfile<?php echo $x;? > ">
<?php
Complete loop
}
?>
<input name= "Uploadneed" type= hidden "value=" <?php echo $uploadNeed;? > ">
<input type= "Submit" name= "submit" value= "Submit" >
</form>
</body>



In this page, I just created an HTML form with the "type" attribute. In the middle of the form, I inserted a section of PHP code to complete the loop. I set the $x to 0 and set the number of loops to the desired value $uploadneed (the value that the user submitted just now). I also output the value of $uploadneed to the hidden field so that it can be passed to the last page.

The purpose of this work is to add the value of the $x variable to the back of the value "UploadFile" of the "name" attribute. The value "UploadFile" of each "name" attribute will then have a different number to distinguish the first file.

Now it's time to finish uploading the last step of the file processfiles.php


<?php
$uploadNeed = $_post[' uploadneed '];
Start loop
for ($x =0; $x < $uploadNeed; $x + +) {
$file _name = $_files[' UploadFile '. $x] [' name '];
Remove special characters from a file name
$file _name = stripslashes ($file _name);
$file _name = Str_replace ("'", "", $file _name);
$copy = Copy ($_files[' UploadFile '. $x] [' tmp_name '], $file _name);
Check if replication is successful
if ($copy) {
echo "$file _name upload success <br/>";
}else{
echo "$file _name upload failure <br/>";
}
}//End loop
?>


In this script, we first get the $uploadneed variable from the uploadform2.php. Like the page on the previous page, we use the "for" statement to also create loops. At different times, we need to use the predefined global variable $_files within the loop, and I passed it to the $file_name variable.

Next, we call the stripslashes and str_replace two functions to handle. Because there may be some special characters in the file name, we use the stripslashes function to prevent an explanation error when uploading.

Notice how i add $x this variable, in $_files $x will become a number. So the program will know which file to process.

When we use the copy function, we really start to copy the uploaded file to the specified current. Finally, a simple code is added to determine the success of the replication and output the results to the screen.

Conclusion

I'm sure this little script will make it easier for you to upload multiple files at once. You can refine it by adding relevant code to the script based on the following hints.

    • Save file name in MySQL database
    • Gets the size of the file and saves it to the database
    • Create a temporary file to save the file name of the file that failed to upload and try again after the program is done
    • Add the ability to bulk delete files


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.