Home > Backend Development > PHP Tutorial > How to accept files and get their suffix using PHP

How to accept files and get their suffix using PHP

PHPz
Release: 2018-10-11 16:01:53
Original
1190 people have browsed it

The HTML form uses the html form to simulate a post request for file upload. The code is as follows:

File UploadSend this File:
Copy after login

Note:

Make sure the file upload form is The attribute is enctype="multipart/form-data", otherwise the file cannot be uploaded

First of all, you need to explain the PHP global variable $_FILES. This array contains all uploaded file information

  • $_FILE['userfile']['name'] : The original name of the client machine file

  • $_FILE['userfile']['type'] : MIME type of file

  • $_FILE['userfile']['size'] : Uploaded file size

  • $_FILE[ 'userfile']['tmpname'] : The temporary file name stored on the server after the file is uploaded

  • $_FILE['userfile']['error'] : Upload with the file Error code

Ideas

1. Generate a 40-digit random string as the file name
2. Transfer the file to different locations depending on whether it is a picture or a voice. File location
3. File size and file type verification will not be performed for the time being

function processFile($files, $type) { 
    $uploadName = null; 
    foreach ($files as $name => $value) { 
      $originalName = $value['name']; 
      $arr = explode(".", $originalName); 
      $postfix = $arr[count($arr) - 1]; 
      $tmpPath = $value['tmp_name']; 
      $tmpType = $value['type']; 
      $tmpSize = $value['size']; 
    } 
     
    $newname = EhlStaticFunction::generateRandomStr(40).".".$postfix; 
     
    switch ($type) { 
      case 1 :  
        // 处理声音文件 
        $destination = VIDEOUPLOADDIR.$newname; 
        break; 
      case 2 : 
        // 处理图像文件 
        $destination = IMAGEUPLOADDIR.$newname; 
        break; 
    } 
     
    move_uploaded_file($tmpPath, $destination); 
  }
Copy after login

For more related tutorials, please visit PHP programming from entry to master full set of videos Tutorial

Related labels:
php
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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template