How to use Thinkphp5+uploadify to upload files

php中世界最好的语言
Release: 2023-03-28 09:14:02
Original
1727 people have browsed it

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

  
Copy after login

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 '找不到对应版本';
    }
  }
}
Copy after login

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!

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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!