Home > Article > Backend Development > How to use Thinkphp5+uploadify to upload files
This time I will show you how to use Thinkphp5 uploadify to implement file upload, what are the precautions for using Thinkphp5 uploadify to implement file upload, the following is a practical case, let’s take a look .
It was my first time to come into contact with server-side development. I tried to make an OTA backend server while learning, and it took a lot of effort to achieve file uploading and progress bar display.
Encountered several problems:
1. Large file upload failed
2.Upload canceled X matches cannot be displayed
3. I don’t know how Pass the variable value to the background php
Record the process:
1. Download the uploadify code to the project, such as public\plug-ins\uploadify Down.
2. The front-end script is as follows.
The client passes the version number in formData. Please see the version_id assignment method. You need to assign it in the controller first.
Cancel matching cannot be displayed, you need to modify the background: url('uploadify-cancel.png') in uploadify.css
Pay attention to the writing of uploader in uploadify
<form enctype="multipart/form-data" method="post" > <input type="file" name="uploadify" id="uploadify" multiple="true" /> </form> <script type="text/javascript"> <?php $timestamp = time();?> var maxSize = 1024 * 1024*1024;//1G $(function() { $('#uploadify').uploadify({ 'debug' : false, <span style="white-space:pre"> </span> 'fileSizeLimit ': maxSize, 'formData' : { 'timestamp' : '<?php echo $timestamp;?>', 'token' : '<?php echo md5('unique_salt' . $timestamp);?>', <span style="white-space:pre"> </span>'version_id': "{$version_id}" }, 'swf' : '/public/plug-ins/uploadify/uploadify.swf', <span style="white-space:pre"> </span>'cancelImg':'/public/plug-ins/uploadify/uploadify-cancel.png', 'uploader' : '{:url("Package/upload")}', <span style="white-space:pre"> </span>'fileTypeDesc' : 'zip文件', <span style="white-space:pre"> </span>'fileTypeExts' : '*.zip', <span style="white-space:pre"> </span> 'multi': false }); }); </script>
3. The back-end script corresponds to the upload function of the controller Package
Pay attention to the method of obtaining the uploaded file. You cannot use the method of obtaining the official Thinkphp5 document.
The saved file name cannot contain special symbols
Modify php.ini: upload_max_filesize = 1024M
post_max_size=48
Restart the service
public function upload(){ $verifyToken = md5('unique_salt' . $_POST['timestamp']); if (!empty($_FILES) && $_POST['token'] == $verifyToken) { $tempFile = $_FILES['Filedata']['tmp_name']; /* $targetFolder = '/public/uploads'; // Relative to the root $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder; $targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name']; // Validate the file type $fileTypes = array('jpg','jpeg','gif','png','zip'); // File extensions $fileParts = pathinfo($_FILES['Filedata']['name']); if (in_array($fileParts['extension'],$fileTypes)) { move_uploaded_file($tempFile,$targetFile); echo '1'; } else { echo 'Invalid file type.'; }*/ $version = model("Version")->retrieve_by_version($_POST['version_id']); if($version){ $file = new File($tempFile,'rw'); $hash_code = $file->hash(); $time = date("Y-m-d-i-s",$_POST['timestamp']); $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.$version['project_name'].DS.$version['version_name'],'update_'.$time.'.zip'); if($info){ // 成功上传后 获取上传信息 echo $info->getExtension(); echo $info->getSaveName(); echo $info->getFilename(); }else{ // 上传失败获取错误信息 echo $file->getError(); } }else{ echo '找不到对应版本'; } } }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to dynamically add, modify, and delete JS arrays and JSON objects
How to use JS Inheritance and multiple inheritance
The above is the detailed content of How to use Thinkphp5+uploadify to upload files. For more information, please follow other related articles on the PHP Chinese website!