For example:
The code is as follows: |
Copy code |
Echo substr (strrchr ($ file_name, '.'), 1 ); |
Or
The code is as follows: |
Copy code |
Echo end (explode (".", $ filename )); |
The most correct method is to use the pathinfo function, for example:
The code is as follows: |
Copy code |
Echo pathinfo ($ filename, PATHINFO_EXTENSION ); |
The pathinfo function supports four types of responses:
PATHINFO_DIRNAME-Directory
PATHINFO_BASENAME-file name (including extension)
PATHINFO_EXTENSION-extension
PATHINFO_FILENAME-file name (excluding the extension)
Example
The code is as follows: |
Copy code |
Function get_extension ($ filename ){ Return pathinfo ($ filename, PATHINFO_EXTENSION ); } |
Call
The code is as follows: |
Copy code |
<? Php Print_r (pathinfo ("/testweb/test.txt ")); ?> |
The following result is output:
Array (
[Dirname] =>/testweb
[Basename] => test.txt
[Extension] => txt
)
If you want to upload a file, you 'd better use the following methods.
$ Extension = $ upfile ['type']; // the file information uploaded by upload.
The code is as follows: |
Copy code |
Switch ($ extension) { Case 'application/msword ': $ Extension = 'doc '; Break; www.111cn.net Case 'application/vnd. ms-excel ': $ Extension = 'XLS '; Break; Case 'application/vnd.openxmlformats-officedocument.wordprocessingml.doc ument ': $ Extension = 'docx '; Break; Case 'application/vnd. ms-powerpoint ': $ Extension = 'ppt '; Break; Case 'application/pdf ': $ Extension = 'PDF '; Break; Case 'application/vnd. Openxmlformats-officedocument.spreadsheetml.sheet ': $ Extension = 'xlsx '; Break; Default: Die ('only Upload doc, docx, xls, pdf, and pptfile <a href = "wend. php"> re-upload </a> '); } |