Home Backend Development PHP Tutorial PHP pathinfo obtains the path, name and other information of the file

PHP pathinfo obtains the path, name and other information of the file

Jul 29, 2016 am 08:46 AM
basename dirname images pathinfo

Suppose there is an image file now, and its server-side path is:
$path = "/www/mywebsite/images/myphoto.jpg";
1.pathinfo() function
pathinfo() function returns a file containing An array of information. There are four elements in the array, namely dirname, basename, extension, and filename. Code for printing array:

Copy code The code is as follows:


$fileArr = pathinfo($path);
print_r($fileArr);
//Output result: Array ( [dirname] => / www/mywebsite/images [basename] => myphoto.jpg [extension] => jpg [filename] => myphoto )


In this way, we only need to get the corresponding key value based on the key name of the array:

Copy the code The code is as follows:


echo $fileArr['filename'];
//Output result: myphoto
echo $fileArr['extension'];
//Output result: jpg
//. ..


2.dirname() function
dirname() function gives a string containing the full path to a file. The value it returns is the directory name after removing the file name, which can be considered as a reference to pathinfo() Extension of the function:

Copy the code The code is as follows:


echo dirname($path);
//Output result: /www/mywebsite/images
//Or
echo dirname("/www/mywebsite /images/");
echo dirname("/www/mywebsite/images");
//The output results are all: /www/mywebsite


So it can be understood that the returned value is the upper directory of the path Address name.
3.basename() function
The basename() function gives a string containing the full path to a file. The value it returns is the basic file name. It can also be considered as an extension of the pathinfo() function:

Copy the code The code is as follows:


echo basename($path);
//Output result: myphoto.jpg
//Or
basename("/www/mywebsite/images/");
// Output result: images


So it can be understood that the returned value is the name of the current directory of the path.

The above introduces the description of the path, name and other information of the file obtained by PHP pathinfo, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

HMD Slate Tab 5G leakes as mid-range tablet with Snapdragon 7s Gen 2, 10.6-inch display and Lumia design HMD Slate Tab 5G leakes as mid-range tablet with Snapdragon 7s Gen 2, 10.6-inch display and Lumia design Jun 18, 2024 pm 05:46 PM

With the Skyline, HMD Global is set to unveil a mid-range smartphone in the style of the Nokia Lumia 920 on July 10. According to the latest information from the leaker @smashx_60, the Lumia design will soon also be used for a tablet, which will be c

Photographer submits real photo to AI image contest, bags the 3rd place before telling Photographer submits real photo to AI image contest, bags the 3rd place before telling Jun 16, 2024 pm 06:46 PM

Oneofthecontestsheldby1839Awardsin2024wasspecialinthatsubmissionstoitweretobegeneratedbyartificialintelligenceratherthancreatedwithacamera.ItwasthiscontestthatpiquedthecuriosityofacertainMilesAstray.Insteadofus

How to configure pathinfo mode in nginx How to configure pathinfo mode in nginx May 12, 2023 pm 07:52 PM

The reason is that I haven’t used Apache for a long time, and I gradually feel unfamiliar with Apache, because my friend has a zendframework framework that was moved from Apache to nginx and requires pathinfo mode support. So I started searching online for articles related to nginx+pathinfo. I thought it would be easy to configure it at first. After searching, I found that there are a lot of articles introducing nginx to enable pathinfo mode, and it seems that it is not difficult. But after several hours, it is still not configured properly. And the contents of a large number of articles are very similar, and they are basically reprinted. Starting to get a little anxious! Because one day has passed and the preparation has not been completed. There is no solution to continue groping, so continue searching. For the convenience of verification, I use a.c

How to configure PATHINFO in Nginx to hide thinkphp index.php How to configure PATHINFO in Nginx to hide thinkphp index.php Jun 03, 2023 pm 01:18 PM

nginx configuration pathinfo hides index.php Put this code in the nginx configuration file server{listen80;default_typetext/plain;root/var/www/html;indexindex.phpindex.htmindex.html;#hide index.phplocation/{if(!- e$request_filename){#First-level directory#rewrite^/(.*)$/index.php/$1last;#Second-level directory rewrite^/myapp/(.*)$/myapp/

PHP returns file path information PHP returns file path information Mar 21, 2024 am 09:01 AM

This article will explain in detail the information about the file path returned by PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP obtains file path information In PHP, you can obtain detailed information of a specified file path through the pathinfo() function. This function returns an array containing information about various parts of the file path. Syntax pathinfo(string$path,int$options=PATHINFO_DIRNAME|PATHINFO_BASENAME|PATHINFO_EXTENSION|PATHINFO_FILENAME) parameter $pat

PHP function introduction—basename(): returns the file name part of the path PHP function introduction—basename(): returns the file name part of the path Jul 27, 2023 pm 08:06 PM

PHP function introduction—basename(): returns the file name part of the path. In PHP programming, it is often necessary to operate on file paths. The basename() function can help us quickly and easily obtain the file name part of the path. This article will introduce the function and usage of the basename() function in detail, and demonstrate its practical application through code examples. The basic syntax of the basename() function is as follows: stringbasename(string

How to enable pathinfo in nginx How to enable pathinfo in nginx May 26, 2023 am 10:43 AM

To transfer apache to nginx, $_server['path_info'] is used on the code side. Pathinfo is not enabled by default for nginx. So we have to manually enable 1, url rewrite location/{//Method 1if(!-e$request_filename){rewrite^/(.*)$/index.php/$1last;break;}}location/{/ /Method 2try_files$uri$uri//index.php$uri;}2, pathinfo sets location~.

How to use the dirname function in PHP to get the directory part of the path How to use the dirname function in PHP to get the directory part of the path Jun 26, 2023 pm 12:02 PM

PHP is a programming language widely used in web development. It provides many built-in functions to facilitate us to deal with various file path issues during development. For example, the dirname function is one of the commonly used path processing functions in PHP. In this article, we will explore how to use the dirname function in PHP to get the directory portion of the path. The dirname function returns the directory portion of the specified path. The syntax of this function is as follows: dirname($path) where $path is a character

See all articles