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.