ThinkPHP combined with AjaxFileUploader to implement the method for uploading non-refreshing files _ php instance

Source: Internet
Author: User
This article mainly introduces ThinkPHP and AjaxFileUploader to implement the upload of Brushless newest files. It is a typical application technique in ThinkPHP file upload and has great practical value, for more information about how to use ThinkPHP and AjaxFileUploader to upload a non-Refresh file, see the example in this article. Share it with you for your reference. The implementation method is analyzed as follows:

First, the AjaxFileUploader plug-in is a jquery-based plug-in. We can use the AjaxFileUploader plug-in to Implement Asynchronous File Upload. Use this plug-in to upload files without worrying about compatibility issues, its Compatibility can be said to be compatible with all mainstream browsers. Next we will introduce an instance of AjaxFileUploader + thinkphp for file upload.

In the ThinkPHP framework, the AjaxFileUploader plug-in is used to upload ajax files. Multiple file formats are supported and the page does not need to be refreshed.

Create the upAction. class. php file in the Lib/Action/directory. The Code is as follows:

The Code is as follows:

<? Php
Class upAction extends BaseAction {
Public function index (){
$ This-> display ();
}

/*
* @ File Upload
* @ Author FineYi
* @ Date 2013-01-23
*/
Public function upLoadFile (){
$ Error = "";
$ Msg = "";
$ FileElementName = 'filetoupload ';
If (! Empty ($ _ FILES [$ fileElementName] ['error']) {
Switch ($ _ FILES [$ fileElementName] ['error']) {
Case '1 ':
$ Error = 'the uploaded file exceeds The upload_max_filesize directive in php. ini ';
Break;
Case '2 ':
$ Error = 'the uploaded file exceeds The MAX_FILE_SIZE direve ve that was specified in the HTML form ';
Break;
Case '3 ':
$ Error = 'the uploaded file was only partially uploaded ';
Break;
Case '4 ':
$ Error = 'no file was uploaded .';
Break;

Case '6 ':
$ Error = 'missing a temporary folder ';
Break;
Case '7 ':
$ Error = 'failed to write file to disk ';
Break;
Case '8 ':
$ Error = 'file upload stopped by extension ';
Break;
Case '20140901 ':
Default:
$ Error = 'no error code avaiable ';
}
} Elseif (empty ($ _ FILES ['filetoupload'] ['tmp _ name']) | $ _ FILES ['filetoupload'] ['tmp _ name'] = 'None '){
$ Error = 'no file was uploaded ..';
} Else {
$ Re = $ this-> up ();
If (! $ Re ){
$ Error = 'up file fail ';
}
$ Msg = $ re ['savename']; // file name
$ Path = '/upload/bizcoop/'. $ msg; // file path
$ Size = $ re ['SIZE']; // File size
}
Echo json_encode (array ('error' => $ error, 'msg '=> $ msg, 'path' => $ path, 'SIZE' => $ size )); exit;
}

Private function up (){
Import ('@. Org. uploadfile'); // copy the upload class UploadFile. class. php to the Lib/Org folder.
$ Upload = new UploadFile ();

$ Upload-> maxSize = '-1'; // The default value is-1, which does not limit the upload size.
$ Upload-> savePath = ICTSPACE_DIST_ROOT_PATH. '/www/upload/bizcoop/'; // save path
$ Upload-> saveRule = uniqid; // rules for saving file names uploaded
$ Upload-> uploadReplace = true; // whether to overwrite a file with the same name
$ Upload-> allowExts = array ('jpg ', 'jpeg', 'png ', 'gif'); // specifies the type of the file to be uploaded.
If ($ upload-> upload ()){
$ Info = $ upload-> getUploadFileInfo ();
Return $ info [0];
} Else {
Return false;
Exit;
}
}
}
?>


Create the index. Tpl file in the/tpl/default/Up/directory. The Code is as follows:

The Code is as follows:


Ajax File Upload Demo


Put the ThinkPHP File Upload class in the/Lib/Org/directory. We need to download some plug-ins from the official website.

I hope this article will help you with PHP programming.

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.