PHP several functions pathinfo (), DirName (), basename ()

Source: Internet
Author: User
$path = "/www/mywebsite/images/myphoto.jpg";
1.pathinfo () function

The pathinfo () function returns an array of file information containing four elements, namely, dirname, basename, extension, filename. To print the array code:

Mixed pathinfo (string $path [, int $options = Pathinfo_dirname | Pathinfo_basename | pathinfo_extension | Pathinfo_filename])

pathinfo ()Returns an associative array containing the information with path. Whether to return an associative array or a string depends on the options.
copy code code as follows:
$FILEARR = PathInfo ($path);
Print_r ($FILEARR);
Output result: Array ([dirname] =>/www/mywebsite/images [basename] => myphoto.jpg [extension] => jpg [filename] => m Yphoto)

So we just have to get the corresponding key value based on the key name of the array:

copy code code as follows:
echo $fileArr [' filename '];
Output Result: Myphoto
echo $fileArr [' extension '];
Output results: JPG
//...

2.dirname () function
The dirname () function gives a string containing a full path to a file, and the value returned removes the directory name after the file name, which can be considered an extension of the PathInfo () function:
copy code code as follows:
echo dirname ($path);
Output Result:/www/mywebsite/images
Or
Echo dirname ("/www/mywebsite/images/");
Echo dirname ("/www/mywebsite/images");
The results of the output are:/www/mywebsite

So it can be understood that the returned value is the directory address name of the upper level of the path.
3.basename () function
The basename () function gives a string containing a full path to a file, and the value he returns is a basic filename, which can also be considered an extension of the PathInfo () function:
copy code code as follows:
Echo basename ($path);
Output Result: myphoto.jpg
Or
BaseName ("/www/mywebsite/images/");
Output result: Images

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.