Use PHP to accept files and get their extension names. _ PHP Tutorial

Source: Internet
Author: User
Use PHP to accept files and get their extension names ,. Use PHP to accept a file and get its suffix. The HTML form uses an html form to simulate a file Upload post request. the code is as follows :! The DOCTYPEhtmlPUBLIC-W3CDTDH uses PHP to accept the file and get its suffix,

HTML form
Use an html form to simulate a post request for File upload. the code is as follows:

           
    File Upload                         


Note:

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


PHP
First, you need to explain the global variable $ _ FILES in PHP. this array contains information about all uploaded FILES.

  • $ _ FILE ['userfile'] ['name']: The original name of the client machine FILE
  • $ _ FILE ['userfile'] ['type']: object MIME type
  • $ _ FILE ['userfile'] ['size']: size of the uploaded FILE
  • $ _ FILE ['userfile'] ['tmpname']: temporary FILE name stored on the server after the FILE is uploaded
  • $ _ FILE ['userfile'] ['error']: error code uploaded to this FILE


Ideas
1. generate a 40-bit random string as the file name
2. save images or audio to different file locations.
3. do not check the file size and file type for the moment.

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: // process the audio file $ destination = VIDEOUPLOADDIR. $ newname; break; case 2: // process image files $ destination = IMAGEUPLOADDIR. $ newname; break;} move_uploaded_file ($ tmpPath, $ destination );}

You can use the following code to obtain the suffix of the uploaded file:

HTML

  
     
   
   


PHP

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

Result example:

The form in HTML simulates a post request for File upload using an html form. the code is as follows :! DOCTYPE html PUBLIC "-// W3C // dtd h...

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.