Php getting file name suffix instance summary _ PHP Tutorial

Source: Internet
Author: User
Tags php file upload
Php obtains the summary of the file name suffix instance. When uploading php files, we need to obtain the file name suffix and then determine the simple file type. in php, the file name suffix acquisition method is very simple and there are many ways, next I will summarize how to determine the type of a simple file after obtaining the suffix of the file name during php file upload. in php, the method for obtaining the suffix of the file name is very simple and there are many ways, here is a summary.

1. basename ()-returned path file name

See the following php code:

The code is as follows:

$ Path = "/usr/www/html/index. php ";
Echo basename ($ path )."
";
// If suffix is selected, the extension is ignored.
Echo basename ($ path, ". php ");
?>


Running result:
Index. php
Index
2. dirname ()-returns the file path of the current script!
Php code:

The code is as follows:


// -- FILE _ returns the complete FILE path
$ Dir = dirname (_ FILE __);
Echo $ dir;
?>

Running result:
F: webzendexercise

3. pathinfo () returns an associated array containing path information.
It includes the following array units: path name dirname, file name basename, and extension name extension.
See the following simple code demonstration:

The code is as follows:
$ Path = "/usr/www/html/index. php ";
$ Pathinfo = pathinfo ($ path );
Echo "directory name: $ pathinfo [dirname]
";
Echo "File name: $ pathinfo [basename]
";
Echo "extension: $ pathinfo [extension]";
?>

Running result:
Directory name:/usr/www/html
File name: index. php
Extension: php
4. realpath -- returns the normalized absolute path name.
The php code is as follows:

The code is as follows:


$ Path = "./exercise/php.txt ";
$ Realpath = realpath ($ path );
Echo $ realpath;
?>

Note one tips: the file path operators in different paths may be different. in windows, you can use "/" and "",
Only "/" can be used in linux, so we recommend that you use "/" during development, such as the file path above!

// Method 1:

The code is as follows:
Function extend_1 ($ file_name)
{
$ Retval = "";
$ Pt = strrpos ($ file_name, ".");
If ($ pt) $ retval = substr ($ file_name, $ pt + 1, strlen ($ file_name)-$ pt );
Return ($ retval );
}

// Method 2
Php code

The code is as follows:
Function extend_2 ($ file_name)
{
$ Extend = pathinfo ($ file_name );
$ Extend = strtolower ($ extend ["extension"]);
Return $ extend;
}


// Method 3
Php code

The code is as follows:
Function extend_3 ($ file_name)
{
$ Extend = explode (".", $ file_name );
$ Va = count ($ extend)-1;
Return $ extend [$ va];
}


// Method 4
Php code

The code is as follows:
Function getFileExt ($ file_name)
{
While ($ dot = strpos ($ file_name, ".")
{
$ File_name = substr ($ file_name, $ dot + 1 );
}
Return $ file_name;
}

?>

...

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.