Using PHP to accept files and get their suffix

Source: Internet
Author: User
This article mainly introduces how to use PHP to accept FILES and obtain their extension names. The author highlights the use of the global variable $ _ FILES. For more information, see

This article mainly introduces how to use PHP to accept FILES and obtain their extension names. The author highlights the use of the global variable $ _ FILES. For more information, see

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.


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:

,

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.