PHP implements multifile Upload. _ PHP Tutorial

Source: Internet
Author: User
PHP implements multifile Upload ,. PHP implements multifile Upload. this example describes how PHP implements multifile Upload. Share it with you for your reference. The specific implementation method is as follows: phpdefine (ROOT, D: Prog PHP implements multifile Upload,

This example describes how to upload multiple files in PHP. Share it with you for your reference. The specific implementation method is as follows:

<? Phpdefine ('root', 'd:/Program Files/www/test/'); class Files_Tool {protected static comment'); public static $ wrong = array (); public static $ path = array (); protected static $ error = array (0 => 'File Upload failed, no error occurred, file uploaded successfully ', 1 => 'File Upload failed. the uploaded file exceeds php. value restricted by the upload_max_filesize option in ini ', 2 =>' file Upload failed. The size of the uploaded file exceeds The value specified by the MAX_FILE_SIZE option in the HTML form ', 3 =>' file Upload failed, only part of the file was uploaded ', 4 =>' file Upload failed, no file was uploaded ', 5 => 'File Upload failed, not allowed suffixes ', 6 =>' file Upload failed, and the temporary folder cannot be found. PHP 4.3.10 and PHP 5.0.3 import ', 7 =>' file Upload failed, file write failed. PHP 5.1.0 introduces ', 8 =>' file Upload failed, the NAME of the form field is not received ', 9 => 'File Upload failed, error unknown '); public static function upload ($ name) {// checks whether the NAME if (! Isset ($ _ FILES [$ name]) {self: $ wrong [] = 8; return false ;} // The 3-dimensional array is simplified to a 2-dimensional array $ files = array_shift ($ _ FILES); // Get the suffix $ files = self: get_Ext ($ files ); // number of file processing times $ n = count ($ files ['name']); for ($ I = 0; $ I <$ n; $ I ++) {// check whether the current file has an error message. if yes, skip the current file and process the next file if ($ files ['error'] [$ I]! = 0) {self ::$ wrong [$ I + 1] = $ files ['error'] [$ I]; continue;} // view the suffix of the current file, allow or not. if not, skip the current file if (! In_array ($ files ['name'] [$ I], self ::$ allowExt) {self ::$ wrong [$ I + 1] = 5; continue ;} // path $ dir = self: time_Dir (); // file name $ name = self: rand_Name (); // suffix $ ext = $ files ['name'] [$ I]; // file location $ path = $ dir. $ name. $ ext; // Move the temporary file. if it fails, skip the current file if (! Move_uploaded_file ($ files ['tmp _ name'] [$ I], $ path) {self ::$ wrong [$ I] = 9; continue ;} // storage path self: $ path [$ I + 1] = strtr ($ path, array (ROOT => '');} return self: $ path ;} // method for obtaining the suffix protected static function get_Ext ($ arr) {if (! Is_array ($ arr) |! Isset ($ arr ['name']) {return false;} foreach ($ arr ['name'] as $ k => $ v) {$ arr ['name'] [$ k] = strtolower (strrchr ($ v ,'. ');} return $ arr;} // Generate the path protected static function time_Dir () {$ dir = ROOT. 'data/images /'. date ('Y/m/d/', time (); if (! Is_dir ($ dir) {mkdir ($ dir, 0777, true);} return $ dir;} // Generate a random file name protected static function rand_Name () {$ str = str_shuffle ('1234567890qwertyuiopasdfghjklzxcvbnm '); $ str = substr ($ str,); return $ str ;}// error interface public static function errors () {foreach (self ::$ wrong as $ k =>$ v) {self ::$ wrong [$ k] = 'Di '. $ k. 'tags '. self: $ error [$ k];} return self: $ wrong ;}}

I hope this article will help you with php programming.

Example: This article describes how PHP implements multifile Upload. Share it with you for your reference. The specific implementation method is as follows: phpdefine ('root', 'd:/Prog...

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.