This article mainly introduces ThinkPHP and AjaxFileUploader in combination to implement the upload of brushless newest files. it is a typical application technique in ThinkPHP file upload and is very useful. if you need ThinkPHP, you can refer to ThinkPHP.
The example in this article describes how ThinkPHP uses AjaxFileUploader to upload a non-refreshing file. 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.