PHP script _ PHP Tutorial for uploading multiple files

Source: Internet
Author: User
Tags file upload script
PHP scripts for uploading multiple files. Note: The original name of this article is CreatingaMulti-FileUploadScriptinPHP. I think this article is generally written, and the code is not very professional, but it is short, and the translator's note at a time: the original name of this article is Creating a Multi-File Upload Script in PHP. I think this article is generally written, and the code is not very professional, but it is short, and I can't find a better article at a time, so I translated it. In fact, there is also a section in the PHP manual about multifile Upload. you can compare them. Some words in the article are not very easy to translate, so some modifications are made according to the original intention. My skills are limited. I hope you can give me more advice.

Introduction

As a PHP programmer, I have met such a customer who needs a form that can upload multiple files at the same time. Therefore, one night I spent an hour and found the best and easiest way to fulfill this requirement. In this tutorial, the "for" loop statement will become the core code.

Script 1: Allows users to set the number of files to be uploaded

UploadForm1.php



File Upload






The preceding HTML code is very simple. In this code, I set the maxlength attribute of the uploadNeed component to 1, and the maximum value that the user can enter is 9. Of course, you can modify this part as needed.

Script 2: create a dynamic form

Well, after completing the following page, we will be half done. We use the for loop statement to complete this task.

UploadForm2.php



File Upload






On this page, I just created an HTML form with the "type" attribute. In the middle of the form, I inserted a PHP code to complete the loop. I set $ x to 0 and set the number of cycles to the required value $ uploadNeed (the value submitted by the user just now ). I also output the $ uploadNeed value to the Den domain so that it can be passed to the last page.

The purpose of this work is to add the value of $ x to the value of uploadFile in the "name" attribute. In this way, each "name" attribute value "uploadFile" is followed by a different number to distinguish the first file.

Script 3: process uploaded files

Now let's complete the last step of file upload.

ProcessFiles. php

$ UploadNeed = $ _ POST ['uploadneed'];
// Start the loop
For ($ x = 0; $ x <$ uploadNeed; $ x ++ ){
$ File_name = $ _ FILES ['uploadfile'. $ x] ['name'];
// Remove special characters from the file name
$ File_name = stripslashes ($ file_name );
$ File_name = str_replace ("'", "", $ file_name );
$ Copy = copy ($ _ FILES ['uploadfile'. $ x] ['tmp _ name'], $ file_name );
// Check whether the copy is successful
If ($ copy ){
Echo "$ file_name uploaded successfully
";
} Else {
Echo "$ file_name Upload failed
";
}
} // End the loop
?>

In this script, we first need to get the $ uploadNeed variable from uploadForm2.php. Like the page on the previous page, we use the "for" statement to create a loop. In different cases, we need to use the predefined global variable $ _ FILES in the loop. I passed it to the variable $ file_name.

Next, we call the stripslashes and str_replace functions for processing. Because the file name may contain some special characters, we use the stripslashes function to prevent interpretation errors during upload.

Note how I add the variable $ x. In $ _ FILES, $ x becomes a number. In this way, the program will know which file to process.

When the copy function is used, the uploaded files are copied to the specified current directory. Finally, a simple code is added to determine whether the replication is successful and output the result to the screen.

Conclusion

I'm sure this little script will make it easier for you to upload multiple files at a time. You can complete the script by adding the relevant code according to the following prompts.

Save the file name in the MySQL database
Get the file size and save it to the database
Create a temporary file to save the name of the file that failed to upload, and try again after the program is executed.
Add the batch file deletion function

Bytes I think this article is generally written and the code is not very professional, but it is short and time-consuming...

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.