Method of Uploading File without Refresh Based on ThinkPHP and AjaxFileUploader

  • 2021-07-24 10:16:51
  • OfStack

This paper describes the method of uploading files without refresh by ThinkPHP combined with AjaxFileUploader. Share it for your reference. The specific implementation method is analyzed as follows:

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

Under the framework of ThinkPHP, AjaxFileUploader plug-in is used to upload ajax files, which supports various file formats and uploads pages without refreshing.

Create the upAction. class. php file in the Lib/Action/directory 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 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'];    // Filename
            $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');// The class will be uploaded UploadFile.class.php Copy to Lib/Org Under the folder
    $upload=new UploadFile();
 
    $upload->maxSize='-1';// Default to -1 Unlimited upload size
    $upload->savePath= ICTSPACE_DIST_ROOT_PATH.'/www/upload/bizcoop/';// Save path
    $upload->saveRule=uniqid;// File name saving rules for uploaded files
    $upload->uploadReplace=true;// Do you want to overwrite if there is a file with the same name
    $upload->allowExts=array('jpg','jpeg','png','gif');// File types allowed to upload
    if($upload->upload()){
        $info=$upload->getUploadFileInfo();
        return $info[0];
    }else{
        return false;
        exit;
    }
}
}
?>

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

<div id="content">
<h1>Ajax File Upload Demo</h1>
<img id="loading" style="display: none;" alt="" src="__APP____PUBLIC__/style/img/loading.gif" />
 
<form action="" enctype="multipart/form-data" method="POST" name="form">
<table class="tableForm" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="input" id="fileToUpload" type="file" name="fileToUpload" size="45" /></td>
</tr>
<tr>
<td><button class="button" id="buttonUpload" onclick="return ajaxFileUpload();">Upload</button></td>
</tr>
</tbody>
<tbody>
<tr>
<td><span> Uploaded attachments :</span></td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
</form></div>

In the/Lib/Org directory under the ThinkPHP file upload class is OK, there are 1 plug-ins we need to download to the official.

I hope this article is helpful to everyone's PHP programming.


Related articles: