Description of php form file iframe asynchronous upload instance,

Source: Internet
Author: User
Tags php form

Description of php form file iframe asynchronous upload instance,

This article provides examples of the Code for asynchronous php form file iframe upload for your reference. The details are as follows:

1. Put the iframe element in the form;
2. when the content of the file upload control changes, the JS setting form action is triggered to process the img_upload_process.php File Uploaded By the file, set the target of the form to iframe, and submit the iframe to the server for file upload;
3. After the files are successfully uploaded in img_upload_process.php, return the paths of the successfully uploaded files to the hidden fields in the form;
4. When the form submission button is clicked, JS sets the form action to the form_process.php file for receiving form data, and the target of the form is set to _ self.

Form: asyn_uplaod.html

<! DOCTYPE html> 

Process file upload: img_upload_process.php

<? Phpinclude 'upload. class. php '; $ file = $ _ FILES ['avator']; $ upload = new Upload (); // if ($ save_path = $ upload-> up ($ file) {// echo <STR <script> upload success parent.doc ument. getElementById ('uploaded _ img '). src = "$ save_path"; saved partition parent.doc ument. getElementById ('loading '). innerHTML = 'uploaded successfully'; your uploaded parent.doc ument. getElementById ('Save _ path '). value = "$ save_path"; </script> STR;} else {$ error = $ upload-> error (); echo <STR <script> specify parameter parent.doc ument. getElementById ('uploaded _ img '). src = ""; Specify your parent.doc ument. getElementById ('loading '). innerHTML = "Upload Failed: $ error"; </script> STR ;}

File Upload tool class: Upload. class. php

<? Phpclass Upload {private $ path; // File Upload directory private $ max_size; // file size limit private $ errno; // error message No. private $ mime = array ('image/jpeg ', 'image/png', 'image/gif '); // file type that can be uploaded/*** constructor, * @ access public * @ param $ path string upload path */public function _ construct ($ path = '. /') {$ this-> path = $ path; $ this-> max_size = 1000000;}/*** File Upload method, store files in different directories * @ access public * @ param $ file array containing the information of uploaded files * @ retur N mixed returns the uploaded file name. if the file fails, false */public function up ($ file) is returned. {// checks whether the file is uploaded through http post to prevent malicious spoofing./* if (! Is_uploaded_file ($ file ['tmp _ name']) {$ this-> errno = 5; // sets the error message number to 5, indicating that the upload is invalid and return false ;} * /// determine whether the file is successfully uploaded from the browser to the server if ($ file ['error'] = 0) {# The file is successfully uploaded to the Temporary Folder, process temporary files // determine the upload type if (! In_array ($ file ['type'], $ this-> mime) {# incorrect type $ this-> errno =-1; return false ;} // determine the file size if ($ file ['SIZE']> $ this-> max_size) {# the file size exceeds the upload limit in the configuration file $ this-> errno =-2; return false;} // get the directory where the uploaded files are stored $ sub_path = date ('ymmd '). '/'; if (! Is_dir ($ this-> path. $ sub_path) {# this directory does not exist. Create mkdir ($ this-> path. $ sub_path);} // rename the file. The file is renamed by the current date + Random Number + suffix $ file_name = date ('ymdhis '). uniqid (). strrchr ($ file ['name'], '. '); // ready. start uploading if (move_uploaded_file ($ file ['tmp _ name'], $ this-> path. $ sub_path. $ file_name) {# return $ sub_path. $ file_name;} else {# failed to move $ this-> errno =-3; return false;} else {# failed to upload data to a temporary folder, set the error code $ this-> e based on the error code Rrno = $ file ['error']; return false ;}/ *** Multifile upload Method * @ access public * @ param $ file array contains an array of uploaded file information, is a two-dimensional array * @ return array an array consisting of successfully returned uploaded file names ,? If there is a failure, it is not easy to handle */public function multiUp ($ files) {// when uploading multiple files, the uploaded file information is a multi-dimensional array, for example, $ _ FILES ['userfile'] ['name'] [0], $ _ FILES ['userfile'] ['name'] [1] // you only need to traverse this array to obtain information about each uploaded file, call the up method in sequence to call foreach ($ files ['name'] as $ key => $ value) {# code... $ file ['name'] = $ files ['name'] [$ key]; $ file ['type'] = $ files ['type'] [$ key]; $ file ['tmp _ name'] = $ files ['tmp _ name'] [$ key]; $ file ['error'] = $ files ['error'] [$ key]; $ file ['SIZE'] = $ files ['SIZE'] [$ key]; // call the up method to complete the upload $ filename [] = $ this-> up ($ file);} return $ filename;}/*** to obtain the error message, get the corresponding error message * @ access public * @ return string returned error message */public function error () {switch ($ this-> errno) {case-1: return 'Check your file type. Currently, the supported types are '. implode (',', $ this-> mime); break; case-2: return 'the file exceeds the size specified by the system and cannot exceed '. $ this-> max_size; break; case-3: return 'file movement failed'; break; case 1: return 'the uploaded file exceeds php. the value restricted by the upload_max_filesize option in ini. Its size is '. ini_get ('upload _ max_filesize '); break; case 2: return' the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form, and its size is '. $ _ POST ['max _ FILE_SIZE ']; break; case 3: return' only part of the file is uploaded '; break; case 4: return' No file is uploaded '; break; case 5: return 'invalid upload'; break; case 6: return 'temporary folder not found '; break; case 7: return 'failed to write the file to the Temporary Folder'; break; default: return 'unknown error, abnormal event'; break ;}}}

Process Form submission: form_process.php

<?phpvar_dump($_REQUEST);var_dump($_FILES);

Click the form submission button:

Code download: php form file iframe asynchronous upload

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.