PHP form file iframe Asynchronous upload instance analysis

Source: Internet
Author: User
Tags php form
This article mainly for you in detail the php form file iframe Asynchronous upload instance, with a certain reference value, interested in small partners can refer to

Specific content is as follows

1. Place the iframe element in the form;
2. File Upload control content changes when the action of the JS setup form to process the file upload img_upload_process.php file, and set the target of the form as an IFRAME, so that the IFRAME to submit to the server for file upload;
3.img_upload_process.php the successful file upload, upload the successfully saved file path back to the hidden fields in the form;
4. When you click the Form Submit button, the JS setup form action is the form_process.php file that receives the form data, and the target of the form is set to _self.

Form: asyn_uplaod.html

<! DOCTYPE html>

Handling file uploads: img_upload_process.php

<?phpinclude ' Upload.class.php '; $file = $_files[' Avator '); $upload = new Upload ();//Upload Tool class object if ($save _path = $upload- >up ($file)) {//upload successful echo <<<str <script>   window.parent.document.getElementById (' Uploaded_ IMG '). src = "$save _path";  Window.parent.document.getElementById (' loading '). InnerHTML = ' upload succeeded ';   Window.parent.document.getElementById (' Save_path '). Value = "$save _path";  </script>str;} else{$error = $upload->error (); Echo <<<str <script>  window.parent.document.getElementById (' Uploaded_img '). src = "";  Window.parent.document.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;//upload file size limit private $errno;//error message number private $mime = Array ( ' Image/jpeg ', ' image/png ', ' image/gif ');//allow upload of file type/** * constructor, * @access public * @param $path string Upload path */public F  Unction __construct ($path = './') {$this->path = $path; $this->max_size = 1000000; }/** * File Upload method, sub-directory storage file * @access public * @param $file array containing the information of the uploaded file * @return mixed successfully returned the uploaded file name, failed to return false */pub Lic function up ($file) {//Determines whether the file is uploaded via HTTP POST, preventing malicious spoofing/* if (! is_uploaded_file ($file [' tmp_name ')]) {$this->err No = 5;  Setting the error message number to 5 indicates that the illegal upload return is false;  }///To determine if the browser is successfully uploaded to the server-side if ($file [' error '] = = 0) {# Upload to Temp folder successfully, processing//upload type for temp file (!in_array ($file [' type '],     $this->mime)) {# type not $this->errno =-1;   return false;    }//Determine file size if ($file [' Size '] > $this->max_size) {# size exceeds the upload limit in the configuration file $this->errno =-2;   return false; }//Gets the directory that holds the uploaded file $sub _path = date (' Ymd '). '   /'; If(!is_dir ($this->path. $sub _path))   {# does not exist for this directory, creation of mkdir ($this->path. $sub _path);   }//File Rename, by the current date + random number + suffix name $file _name = Date (' Ymdhis '). Uniqid (). STRRCHR ($file [' name '], '. '); Ready to start uploading if (Move_uploaded_file ($file [' Tmp_name '], $this->path. $sub _path. $file _name)) {# move successfully return $ Sub_path.   $file _name;    } else {# move failed $this->errno =-3;   return false;   }} else {# Upload to Temp folder failed, set error number according to its error number $this->errno = $file [' ERROR '];  return false; }}/** * Multiple file Upload method * @access public * @param $file array containing the uploaded file information is a two-dimensional array * @return Array successfully returned the uploaded filename, if there is a failure Good deal. */Public Function multiup ($files) {//upload file information in multiple file uploads is a multidimensional array, such as $_files[' UserFile ' [' Name '][0],$_files['   UserFile ' [' Name '][1]//We just need to iterate through the array, get the information for each uploaded file, call the up method in turn to 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; /** * Get the error message and get the appropriate error prompt based on the error number * @access public * @return String returns an error message */Public Function error () {switch ($this-    errno) {case-1: return ' Please check your file type, currently supported types are '. Implode (', ', $this->mime);   Break Case-2: Return ' file exceeds the system specified size, maximum cannot exceed '.    $this->max_size;   Break    Case-3: Return ' file move failed ';   Break    Case 1:return ' uploaded file exceeds the value of upload_max_filesize option limit in php.ini, and 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 ' file is only partially uploaded ';   Break    Case 4:return ' No files were uploaded ';   Break    Case 5:return ' illegally uploaded ';   Break    Case 6:return ' Unable to find temp folder ';   Break    Case 7:return ' file written to temp folder failed ';   Break    Default:return ' unknown error, supernatural event ';  Break } }}

Process form submission: form_process.php

<?phpvar_dump ($_request); Var_dump ($_files);

Click the Form Submit button result:

Code Download: PHP form file iframe asynchronous upload

Related Article

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.