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: