Home  >  Article  >  Backend Development  >  How to use pathinfo() to obtain path array in PHP

How to use pathinfo() to obtain path array in PHP

autoload
autoloadOriginal
2021-04-13 15:06:571826browse

How to use pathinfo() to obtain path array in PHP

In the process of using PHP, the path of the file is more commonly used, so how to put the path information of the file into an array for better use? ? We mainly use the pathinfo() function.

The syntax of pathinfo:

pathinfo ( string $path , [int $options] )
  • $path: The path to be parsed

  • $options:

If empty: Return all path information.

If there is a value: PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION or PATHINFO_FILENAME.

  • Return value: If $option is empty, an associative array containing path information is returned. If not empty, returns a string (content depends on $options).

1. Only the parameter $path:

<?php
    $path="/home/ramki/ramki.pdf";
    echo "<pre class="brush:php;toolbar:false">";
    print_r(pathinfo($path));
?>

The output result is as follows:

Array
(
    [dirname] => /home/ramki
    [basename] => ramki.pdf
    [extension] => pdf
    [filename] => ramki
)

2.If $options=PATHINFO_DIRNAME

<?php
    $path="/home/ramki/ramki.pdf";
    echo "<pre class="brush:php;toolbar:false">";
    print_r(pathinfo($path,PATHINFO_DIRNAME));
    // 输出结果:/home/ramki
?>

3.If $options=PATHINFO_BASENAME

<?php
    $path="/home/ramki/ramki.pdf";
    echo "<pre class="brush:php;toolbar:false">";
    print_r(pathinfo($path,PATHINFO_BASENAME));
    // ramki.pdf
?>

4.If $options=PATHINFO_EXTENSION

<?php
    $path="/home/ramki/ramki.pdf";
    echo "<pre class="brush:php;toolbar:false">";
    print_r(pathinfo($path,PATHINFO_EXTENSION));
    // pdf
?>

5.If $options=PATHINFO_FILENAME

##​ This constant only applies to PHP>=5.2.0

<?php
    $path="/home/ramki/ramki.pdf";
    echo "<pre class="brush:php;toolbar:false">";
    print_r(pathinfo($path,PATHINFO_FILENAME));
    // ramki
?>

Recommended: 2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of How to use pathinfo() to obtain path array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn