The difference and example of dirname,basename,pathinfo function in PHP

Source: Internet
Author: User
To get the path, directory, or file name of the file in PHP, we often use the three functions of the dirname (), basename (), PathInfo (), which are described in the previous article, respectively, for each of the three functions. This article mainly introduces to you in detail

The difference between these three functions and the usage examples.

DirName () function

String DirName (String $path)

The PHP dirname function gets the directory portion of the given file path, and the parameter $path is a string of file paths

The DirName () function is often used with the magic variable __file__, and the Magic variable __file__ represents the full path and file name of the currently running file.

DirName (DirName (__file__)); Get the file on the top of the directory name

DirName (__file__); Gets the directory name of the file.

Such as:

<?phpecho dirname ("c:/testweb/home.php"). " <br/> "; Echo dirname ("/testweb/home.php ")." <br/><br/> "; Echo __file__." <br/> "; Echo dirname (__file__)." <br/> "; Echo dirname (DirName (__file__));? >

Code Run Result:

BaseName () function

String basename (String $path [, String $suffix])

The PHP basename () function gets the file name portion of the path, which is exactly the opposite of DirName () (DirName gets the directory portion of the path).

The first parameter, $path, represents a string that contains a full path to a file, and the second parameter indicates that if the file name ends in suffix, that part will be removed.

Examples are as follows:

<?phpvar_dump (basename ("/ETC/SUDOERS.D", ". D")), Var_dump (basename ("/etc/passwd")), Var_dump (basename ("/etc/") ); Var_dump (BaseName (".")); Var_dump (basename ("/"));? >

Code Run Result:

PathInfo () function

The PHP pathinfo function parses the path and resolves the path to an array of four values, including the directory name, the full filename, the file extension, and the file name (not including the file suffix), and the key names for the four values are dirname, basename, Extension and filename, we can get the value of the directory name, the full filename, the file extension, and the file name through the four key names.

Grammar:

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

Parameters:

Path to be resolved by path.

Options if specified, the specified elements are returned; They include: Pathinfo_dirname,pathinfo_basename and Pathinfo_extension or Pathinfo_filename. If you do not specify options, the default is to return all cells.

Instance:

< $test = PathInfo ("http://localhost/index.php");p Rint_r ($test); >

Code Run Result:

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.