Home > Backend Development > PHP Tutorial > ThinkPHP combines AjaxFileUploader to achieve refresh-free file upload, _PHP tutorial

ThinkPHP combines AjaxFileUploader to achieve refresh-free file upload, _PHP tutorial

WBOY
Release: 2016-07-13 10:15:46
Original
1060 people have browsed it

ThinkPHP combined with AjaxFileUploader to achieve refresh-free file upload,

The example in this article describes the method of using ThinkPHP combined with AjaxFileUploader to achieve refresh-free file upload. Share it with everyone for your reference. The specific implementation method is analyzed as follows:

First of all, the AjaxFileUploader plug-in is a plug-in based on jquery. We can use the AjaxFileUploader plug-in to realize the asynchronous file upload function. Don’t worry about compatibility issues when using this plug-in to upload files. Its compatibility can be said to be compatible with all mainstream browsers. Tools, let's introduce to you an example of AjaxFileUploader+thinkphp implementing file upload.

Using the AjaxFileUploader plug-in under the ThinkPHP framework to implement ajax file upload, supporting multiple file formats and uploading without page refresh.

Create the upAction.class.php file in the Lib/Action/ directory with the following code:

Copy code The code is as follows:
class upAction extends BaseAction{
public function index(){
    $this->display();
}
 
/*
*@文件上传
*@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 directive 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 '999':
            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'];    //文件名
            $path = '/upload/bizcoop/'.$msg;    //文件路径
            $size = $re['size'];    //文件大小
    }       
    echo json_encode(array('error'=>$error,'msg'=>$msg,'path'=>$path,'size'=>$size));exit;
}
 
private function up(){
    import('@.Org.UploadFile');//将上传类UploadFile.class.php拷到Lib/Org文件夹下
    $upload=new UploadFile();
 
    $upload->maxSize='-1';//默认为-1,不限制上传大小
$upload->savePath= ICTSPACE_DIST_ROOT_PATH.'/www/upload/bizcoop/';//Save path
$upload->saveRule=uniqid;//File name saving rules for uploaded files
$upload->uploadReplace=true;//Whether to overwrite if a file with the same name exists
$upload->allowExts=array('jpg','jpeg','png','gif');//File types allowed 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:

Copy code The code is as follows:

Ajax File Upload Demo

























Uploaded attachments:

Just put the ThinkPHP file upload class in the /Lib/Org/ directory. There are some plug-ins that we need to download from the official website.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/902782.htmlTechArticleThinkPHP combined with AjaxFileUploader to achieve refresh-free file upload. This article describes the example of ThinkPHP combined with AjaxFileUploader to achieve refresh-free file upload. method. Share it with everyone for everyone...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template