How to use thinkphp5+uploadify for file uploads

Source: Internet
Author: User
This time to bring you how to use thinkphp5+uploadify to achieve file upload, the use of thinkphp5+uploadify to achieve file upload notes are what, the following is the actual case, together to see.

First Contact server-side development, learning while trying to do an OTA backend server, a great effort to achieve the file upload and progress bar display.

Encounter several problems:

1. Large File upload failed
2. Upload Cancel x match display not come out
3. Do not know how to pass the variable value to the background php

Record the process:

1. Download the uploadify code into the project, such as public\plug-ins\uploadify .
2. The front-end script is as follows

The client passes the version number in the Formdata, see the version_id assignment method, need to give it assign in the controller first.

Cancel compliance display, need to modify the uploadify.css insidebackground: url('uploadify-cancel.png')

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 = 1024x768 * 1024*1024;//1g $ (function () {$ (' #uploadify '). Uploadify ({' Debug ': false, <s Pan style= "White-space:pre" > </span> ' filesizelimit ': maxSize, ' formData ': {' timestamp ': ' &lt ;? 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:p Re "> </span> ' filetypedesc ': ' Zip file ', <span style=" white-Space:pre "> </span> ' filetypeexts ': ' *.zip ', <span style=" White-space:pre "> </span> ' multi '  : false}); });</script>

3. Back-end script corresponds to the upload function of the controller package

Note the method of obtaining the upload file, you cannot use the THINKPHP5 official document that way.

Save file names cannot be with special symbols

Modify PHP.ini: Restart upload_max_filesize = 1024M post_max_size=48 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) {//upload information after successful upload echo $info->getextension ();        echo $info->getsavename ();      echo $info->getfilename ();      }else{//upload failed to get error message echo $file->geterror ();    }}else{Echo ' could not find the corresponding version '; }  }}

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

How to make JS array and JSON object dynamic implementation Add, modify, delete

How to use JS inheritance and multiple inheritance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.