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

autoload
Release: 2023-03-09 10:50:02
Original
1841 people have browsed it

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] )
Copy after login
  • $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));
?>
Copy after login

The output result is as follows:

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

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
?>
Copy after login

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
?>
Copy after login

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
?>
Copy after login

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
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template