PHP + ajaxForm asynchronous upload file instance with progress bar,

Source: Internet
Author: User

PHP + ajaxForm asynchronous upload file instance with progress bar,

Before using the ajaxForm method, install the form. js plug-in, which is available online;

I. First of all, in usage, ajaxForm can receive 0 or 1 parameter. This parameter can be a variable, an object, or a callback function. This object has the following parameters:

Var object = {
Url: url, // address of the data submitted by form
Type: type, // form submission method (method: post/get)
Target: target, // The element Id of the response data returned by the server
BeforeSerialize: function () {}// serialize the callback function before data is submitted.
BeforeSubmit: function () {}, // callback function executed before submission
Success: function () {}, // callback function executed after submission is successful
Error: function () {}, // function that fails to be submitted
DataType: null, // type of data returned by the server
ClearForm: true, // whether to clear the field values in the form after the submission is successful
RestForm: true, // whether to reset the field value in the form after the submission is successful, that is, it is restored to the status when the page is loaded.
Timeout: 6000 // sets the request time. After this time is exceeded, the request is automatically exited, in milliseconds ).

}

AjaxForm js code
$(function(){   $("form").ajaxForm(object);})

Code of the Instance

Htmlcode

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

Php uploads the upload class upload. class. php file

<? Phpdate_default_timezone_set ("PRC"); // set the time zone // upload class upload {protected $ file_path = "files"; // The current files storage folder protected $ file_size = 5120000; // 5 M User upload/*** check whether the file is empty */public function check_file ($ get_file) {if (empty ($ get_file )) {$ type = "check_file"; $ arr = array ('error' => 'empty _ name', 'type' => $ type); echo json_encode ($ arr ); exit ();} return true;}/*** check file type */public function check_type ($ ge T_type) {if ($ get_type = ". docx ") | ($ get_type = ". doc ") {// you can add else {$ type =" check_type "; $ arr = array ('error' => 'format ', 'type' => $ type); echo json_encode ($ arr); exit ();} return true ;} /*** check file size */public function check_size ($ get_file) {if ($ get_file! = "") {If ($ get_file> $ this-> file_size) {$ arr = array ('error' => 'large'); echo json_encode ($ arr ); exit () ;}} else {return false; exit () ;}return true ;}/ *** File Save */public function save_file ($ file_type, $ file_tmp_name) {$ rand = rand (1000,999 9); $ pics = date ('ymdhis '). $ rand. $ file_type; $ path = $ this-> file_path. "/". $ pics; $ result = move_uploaded_file ($ file_tmp_name, $ path); if ($ result) {re Turn $ pics;} else {return false; exit () ;}}?>

Ajax submits php processing file upload. php

<? Phpinclude ("upload. class. php "); $ up_obj = new upload (); // get the upload File name $ get_fileName = $ _ FILES ['mypic '] ['name']; $ get_fileSize = $ _ FILES ['mypic '] ['SIZE']; $ get_TmpFiles = $ _ FILES ['mypic'] ['tmp _ name']; $ get_fileType = strstr ($ get_fileName ,'. '); $ check_result = $ up_obj-> check_file ($ get_fileName); if ($ check_result) {$ result_type = $ up_obj-> check_type ($ get_fileType ); // check the file type if ($ result_type) {$ result_size = $ up_o Bj-> check_size ($ get_fileSize); // check the file size if ($ result_size) {$ pics = $ up_obj-> save_file ($ get_fileType, $ get_TmpFiles ); // file upload and save $ size = round ($ get_fileSize/, 2); $ arr = array ('name' => $ get_fileName, 'pic '=> $ pics, 'SIZE' => $ size, 'error' => 2, 'LIST' => $ _ POST ['LIST']); if ($ pics) {// check the file upload status echo json_encode ($ arr) ;}}}?>

Instance file Baidu Network Disk: http://pan.baidu.com/s/1cdFXmu

Password: d1fr

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.