Home > Backend Development > PHP Tutorial > PHP method to accept files and get suffix name

PHP method to accept files and get suffix name

墨辰丷
Release: 2023-03-30 15:26:02
Original
2294 people have browsed it

This article mainly introduces the method of using PHP to accept files and obtain their suffix names. The author focuses on the use of the $_FILES global variable. Friends in need can refer to

HTML form form
Use an HTML form to simulate a post request for file upload. The code is as follows:

  <!DOCTYPE html> 
  <html> 
  <head> 
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  <title>File Upload</title> 
  </head> 
  <body> 
   
  <form enctype="multipart/form-data" action="test.php" method="POST"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> 
    Send this File:<input name="userfile" type="file"/> 
    <input type="submit" value="Send File" /> 
  </form> 
   
   
  </body> 
  </html>
Copy after login


Note:

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


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

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

  • $_FILE['userfile']['type'] : The file's original name MIME type

  • $_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'] : and the error code for uploading the file


Ideas
1. Generate a 40-digit random string as the file name
2. Transfer the file to different file locations depending on whether it is a picture or a voice
3. No verification of file size and file type is required for the time being.

  function processFile($files, $type) { 
    $uploadName = null; 
    foreach ($files as $name => $value) { 
      $originalName = $value[&#39;name&#39;]; 
      $arr = explode(".", $originalName); 
      $postfix = $arr[count($arr) - 1]; 
      $tmpPath = $value[&#39;tmp_name&#39;]; 
      $tmpType = $value[&#39;type&#39;]; 
      $tmpSize = $value[&#39;size&#39;]; 
    } 
     
    $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

To obtain the suffix name of the uploaded file, you can use the following code:

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title></title>
  <meta name="keywords" content=" keywords" />
  <meta name="description" content="description" />
</head>
<body>
  <form method="post" action="" enctype="multipart/form-data">
  <input type="file" name="upfile" size="20" />
  <input type="submit" name="submit" value="submit" />
  </form>
</body>
</html>
Copy after login


PHP

<?PHP
  if(isset($_POST[&#39;submit&#39;])) {
    $string = strrev($_FILES[&#39;upfile&#39;][&#39;name&#39;]);
    $array = explode(&#39;.&#39;,$string);
    echo $array[0];
  }  
?>
Copy after login

Result example:

20158591330708.jpg (423×167)

Summary: The above is the entire content of this article, I hope it can be helpful to everyone learning helps.

Related recommendations:

PHP uses preg_split and explode to implement the method of splitting textarea to store content

PHP implementation File lock locking and unlocking methods

PHP method to implement the automatic exit function of users who have not operated for 30 minutes based on cookies

The above is the detailed content of PHP method to accept files and get suffix name. For more information, please follow other related articles on the PHP Chinese website!

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