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...