Upload multiple files to the server instance in PHP-PHP source code

Source: Internet
Author: User
In php, if we want to upload multiple files, we only need to traverse the form and basically achieve half of it. The following small series will introduce how to upload multiple files to the server instance in PHP, I hope this method will be useful to you. In php, if we want to upload multiple files, we only need to traverse the form and basically achieve half of it. The following small series will introduce how to upload multiple files to the server instance in PHP, I hope this method will be useful to you.

Script ec (2); script

Instance description

Uploading images to the server is an essential feature in program development. It not only achieves the purpose of image sharing, but also increases the Website access volume and enriches the website content. In this example, we will explain how to use the POST method to upload multiple images.

Key Technologies

The key to Multifile upload is how to define the name of the uploaded file element and how to determine the number of uploaded files. In this example, the name of the uploaded file is defined as an array (the name of the uploaded file is "files []"). To upload any number of images (within four images), array_filter () and callback functions are used to remove empty elements from the array during file uploading.

The array_filter () function uses the callback function to filter elements in the array. The syntax is as follows:
Array array_filter (array input [, callback])


The array_filter () function transmits each value in the input array to the callback function in sequence. If the callback function returns TRUE, the current value of the input array is included in the returned result array, and the key name of the array remains unchanged.
Note: do not modify the array in the callback function. For example, add or delete elements in the array. If the array changes, then the function is useless. If the callback () function is not provided, array_filter () will delete the callback function defined in the instance in the input file as check () to verify whether the element value in the array is null. Its syntax is as follows:

The Code is as follows:


Function check ($ var) {// verify whether the returned value of the array is null
Return ($ var! = "");
}

Note: The POST method is used to upload multiple images. When creating a form, you must specify the enctype = "multipart/form-data" attribute. If you want to control the size of the uploaded file by hiding the value of MAX_FILE_SIZE, the file must be hidden before the file domain to be uploaded. Otherwise, the file will not work.
Design Process


(1) Create the index. php file. Add a form, set the file field and submit button, use the POST method, set enctype = "multipart/form-data", submit the data to the index_ OK .php page, and complete the upload of multiple files, the key code is as follows:

The Code is as follows:




(2) In the index. php file, connect to the database and read the data stored in the database to achieve paging output of the uploaded files. For the code, see related content on the CD.
(3) create an index. php file to obtain the data submitted in the form, store multiple files to the server, and store the file name and storage path to the database. The Code is as follows:

The Code is as follows:


Header ("Content-type: text/html; charset = UTF-8"); // sets the file encoding format
Include "conn/conn. php"; // contains the database link file
If ($ _ POST [files]! = ""){
If (! Is_dir ("./upfile ")){
Mkdir ("./upfile"); // create an upload file storage folder
}
$ Data = date ("Y-m-d H: m: s"); // defines the time
Function check ($ var) {// verify whether the returned value of the array is null
Return ($ var! = ""); // Returns an array element if it is not null.
}
$ Files = array_filter ($ _ POST ["files"], "check"); // Remove empty array values
$ Array = array_filter ($ _ FILES ["picture"] ["name"], "check"); // Remove empty array values
Foreach = ($ aarray as $ key => value) {// read the data in the array cyclically
$ Path = 'upfile/'. time (). $ key. strtolower (strstr ($ value, "."); // defines the storage location of the uploaded file
Move_uploaded_file ($ _ FILES ["picture"] ["tmp_name"] [$ key], $ path); // perform the upload operation
$ Query = "insert into tb_up_file (file_test, data, file_name) values ('$ path',' $ data' $ files [$ key] ')";
$ Result = mysql_query ($ query );
}
Echo "script"
Alert('upload success: 'upload success registry.location.href}'index.html '; script ";
}
?>

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.