Home>

First of all, the ajaxfileuploader plugin is a jquery-based plugin. We can use the ajaxfileuploader plugin to implement the asynchronous file upload function.Do n’t worry about compatibility issues when uploading files with this plugin,Its compatibility can be said to be compatible with all major browsers,Let's introduce an example of file uploading with ajaxfileuploader + thinkphp.

Use the ajaxfileuploader plugin to implement ajax file upload under thinkphp framework.Supports multiple file formats,The page was uploaded without refresh.

Create upaction.class.php file in lib/action/directory, 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 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"];//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 is -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;//Whether to overwrite if a file with the same name exists
 $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, the code is as follows:

<div>
<h1>ajax file upload demo</h1>
<img src="__ app ____ public __/style/img/loading.gif" />
<form action="" enctype="multipart/form-data" method="post" name="form">
<table cellspacing="0" cellpadding="0"&​​gt;
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="file" name="filetoupload" size="45" /></td>
</tr>
<tr>
<td><button onclick="return ajaxfileupload ();">upload</button></td>
</tr>
</tbody>
<tbody>
<tr>
<td><span>Uploaded Attachment:</span></td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
</form></div>

Put the thinkphp file upload class in the/lib/org/directory.There are some plugins we need to go to the official download.

php
  • Previous An instance of JavaScript's current height
  • Next Laravel 4 installation and entry graphic tutorials