How PHP accepts files and obtains suffix names

Source: Internet
Author: User
Tags explode
This article mainly introduces the use of PHP to accept files and get the prefix name of the method, the author highlighted the use of $_files global variables, the need for friends can refer to the following

Form form for HTML
The HTML form simulates a POST request for a file upload, with the following code:

  <! DOCTYPE html>   


Attention:

To ensure that the file upload form's properties are Enctype= "Multipart/form-data", the file cannot be uploaded


Php
First, you need to explain the PHP global variable $_files, which contains all the 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 ']: temporary file names stored on the server after files are uploaded

    • $_file[' userfile ' [' Error ']: and the file upload error code


Ideas
1. Generate 40-bit random string as file name
2, depending on whether the file is a picture or voice transferred to a different file location
3, temporarily do not do file size and file type verification

  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:          //processing sound file         $destination = Videouploaddir. $newname;         break;       Case 2:         //Processing image file         $destination = Imageuploaddir. $newname;         break;     }          Move_uploaded_file ($tmpPath, $destination);   }

You can use the code to get the suffix of the uploaded file:

Html

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


Php

<? PHP  if (isset ($_post[' submit ')) {    $string = Strrev ($_files[' upfile ' [' name ']);    $array = Explode ('. ', $string);    echo $array [0];  }  ? >

Examples of results:

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP uses Preg_split and explode to implement the method of partitioning textarea storage content

PHP implementation of file lock locking, unlocking method

PHP implementation method based on cookie to set the user to not operate the auto Exit function for 30 minutes

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.