PHP implements Multifile upload sample code,

Source: Internet
Author: User

PHP implements Multifile upload sample code,

Upload multiple files

1. Before operating multi-file information, you need to build file information and convert the 3D array into a two-dimensional array that facilitates file operations through the buildInfo function.

Function buildInfo () {$ I = 0; foreach ($ _ FILES as $ v) {// determine whether the file is a single file if (is_string ($ v ['name']) {$ files [$ I] = $ v; $ I ++;} else {// multi-file foreach ($ v ['name'] as $ key => $ value) {$ files [$ I] ['name'] = $ value; $ files [$ I] ['SIZE'] = $ v ['SIZE'] [$ key]; $ files [$ I] ['tmp _ name'] = $ v ['tmp _ name'] [$ key]; $ files [$ I] ['type'] = $ v ['type'] [$ key]; $ files [$ I] ['error'] = $ v ['error'] [$ key]; $ I ++ ;}} return $ files ;}

Single files and multi-files are determined by the file array type, single files (two-dimensional arrays), and multiple files (three-dimensional arrays ).

2. File Upload functions and Parameters

Copy codeThe Code is as follows:
Function uploadFiles ($ path = "uploads", $ allowExt = array ("jpg", "gif", "png", "wbmp"), $ maxSize = 1048576, $ imgFlag = true)

Path to save the directory. AllowExt, which allows an array of extensions. MaxSize: the maximum size of uploaded files. ImgFlag, the identifier of the image file.

3. Check whether the directory of the saved file exists.

if(!file_exists($path)){    mkdir($path, 0777, true);  }

4. input the constructed file information and save it to files

$i = 0;$files = buildInfo();

5. Identify the file error Type

Foreach ($ files as $ file) {if ($ file ['error'] = UPLOAD_ERR_ OK) {} else {switch ($ file ['error']) {case 1: $ mes = "exceeds the size of the configuration file to be uploaded"; break; case 2: $ mes = "exceeds the form setting"; break; case 3: $ mes = "file partially uploaded"; break; case 4: $ mes = "No File Uploaded"; break; case 6: $ mes = "no temporary directory found "; break; case 7: $ mes = "files cannot be written"; break; case 8: $ mes = "File Upload interrupted due to PHP extensions"; break ;}

6. Restrict File Types

If ($ file ['error'] = UPLOAD_ERR_ OK) {$ ext = getExt ($ file ['name']); if (! In_array ($ ext, $ allowExt) {exit ("invalid file type ");}

7. Restrict true image types

If ($ imgFlag) {if (! Getimagesize ($ file ['tmp _ name']) {exit ("not a real image type ");}}

8. Determine the file size

If ($ file ['SIZE']> $ maxSize) {exit ("the file is too large ");}

9. Determine whether the file is uploaded through post

If (! Is_uploaded_file ($ file ['tmp _ name']) {exit ("the file is not uploaded through http post ");}

10. Rename the file (unique and unique) and save it to the target directory.

$ Filename = getUniName (). ". ". $ ext; $ destination = $ path. "/". $ filename; if (move_uploaded_file ($ file ['tmp _ name'], $ destination) {$ mes = "file Uploaded successfully "; $ file ['name'] = $ filename; unset ($ file ['error'], $ file ['tmp _ name']); $ uploadedFiles [$ I] = $ file; $ I ++ ;}

Override the name in the file information array and delete the variables file ['error'] and file ['tmp _ name']. Finally, save the successfully uploaded files to the uploadedFiles array.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.