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:
,