


Detailed explanation of the php pathinfo() function to obtain the path, name and other information of the file
In PHP, it is very easy to obtain the path, name, or extension of a file through functions. You can use the php pathinfo() function we talked about earlier or you can use the php dirname() function, php basename() function and other ways to obtain the corresponding information.
PHP pathinfo() function
Definition and usage
pathinfo() function returns the file path in the form of an array or string Information.
The returned array elements are as follows:
[dirname]: Returns the directory part in the file path
[basename]: Returns the part of the file name in the file path
[extension]: Returns the file in the file path Part of the type
Syntax
pathinfo(path,options)
Parameters | Description |
---|---|
path | Required. Specifies the path to be checked. |
options | Optional. Specifies the array elements to be returned. The default is all. Possible values:
|
##Tips and comments
Comments: If Instead of requesting all elements, the pathinfo() function returns a string.php turns on pathinfo routing mode: pathinfo mode requires php.ini to turn on the following parameter
cgi.fix_pathinfo=1
Example 1
<?php print_r(pathinfo("/testweb/test.txt")); ?>
<?php var_dump(pathinfo("/testweb/test.txt",PATHINFO_DIRNAME)); var_dump(pathinfo("/testweb/test.txt",PATHINFO_BASENAME)); var_dump(pathinfo("/testweb/test.txt",PATHINFO_EXTENSION)); ?>
$path = "/www/mywebsite/images/myphoto.jpg";
1.pathinfo() function
pathinfo() function returns a file containing file information Array, there are four elements in the array, namely dirname, basename, extension, filename. The code to print the array:
<?php $path = "/www/mywebsite/images/myphoto.jpg"; $fileArr = pathinfo($path); print_r($fileArr); ?>
The code is as follows:
<?php $path = "/www/mywebsite/images/myphoto.jpg"; $fileArr = pathinfo($path); echo $fileArr['filename']."<br/>"; //输出结果:myphoto echo $fileArr['extension']; //输出结果: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 Extension of the pathinfo() function:
<?php $path = "/www/mywebsite/images/myphoto.jpg"; $fileArr = pathinfo($path); echo dirname($path)."<br/>"; //输出结果:/www/mywebsite/images //或者 echo dirname("/www/mywebsite/images/")."<br/>"; echo dirname("/www/mywebsite/images"); //输出的结果都为:/www/mywebsite ?>
3.basename() function
The basename() function gives a full name that points to a file. The path string, the value returned is the basic file name, which can also be considered as an extension of the pathinfo() function:
<?php $path = "/www/mywebsite/images/myphoto.jpg"; $fileArr = pathinfo($path); echo basename($path)."<br/>"; //输出结果:myphoto.jpg //或者 echo basename("/www/mywebsite/images/"); //输出结果:images ?>
【Recommended related articles】:
1. Detailed explanation of the php pathinfo() function to obtain file path information
2. Detailed explanation of the usage of php dirname() function to obtain file information
3. Detailed explanation of php Usage of basename() function to obtain file name
The above is the detailed content of Detailed explanation of the php pathinfo() function to obtain the path, name and other information of the file. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Where can I find win7 desktop file path and win7 desktop file path? When our computer lasts forever, it gets stuck. As we have more and more files, the computer will run slower and slower. So where is the win7 desktop file path? Let us share the tutorial. 1. First, we open the C drive folder. 2. Then find the desktop, right-click and select Properties. 3. The properties box pops up, select the shortcut, and there is the target file path below. The above is the search method for the win7 desktop file path. I hope it will be helpful to everyone.

In the Python programming process, a common error is "FileNotFoundError", which is the error that the file is not found. This error usually occurs when you try to read or write a file that does not exist. This error message will appear in the terminal window or Python interactive environment. This question can be confusing for beginners. So, this article will introduce some methods to solve this problem and help you find and solve this error. Check whether the file path is correct by checking the file

Dealing with Chinese encoding issues in file paths is a common challenge in Java development, especially when it comes to operations such as file uploading, downloading, and processing. Since Chinese characters may have different representations under different encoding methods, if they are not processed correctly, problems such as garbled characters or unrecognizable paths may occur. This article will discuss how to correctly handle the Chinese encoding problem of file paths in Java development. First, we need to understand how coding is done in Java. Java internally uses the Unicode character set to represent characters. and

In Java development, dealing with special characters in file paths is a common challenge. Special characters in file paths include slash, backslash, colon, etc. These characters have different meanings in different operating systems, so special processing is required when processing file paths. This article will introduce some common file path special character problems and provide corresponding solutions. 1. Slash and backslash issues In Windows systems, file paths use backslashes as delimiters, for example: C:\Usersesti

The methods to obtain the file path in C++ are: 1. Use the std::filesystem library. 2. Use Boost library. These methods can be used to get the absolute path, root directory, parent directory, and extension of a file. In practice, these techniques can be used to display file lists in user interfaces.

Solution to the problem that the path cannot be found for Python file download: Make sure the download path exists and has write permission. Checks whether the user has write permission to the file in the specified path. If using relative paths, make sure they are relative to the current working directory. Use the os.path.abspath() function to convert a relative path to an absolute path.

How to display the file path in Linux In the Linux operating system, you can use some simple commands to view the path of a file. These commands can help users quickly locate the location of files and facilitate file management and operation. The following will introduce several methods of displaying file paths, with specific code examples. 1. Use the pwd command. The pwd command can display the absolute path of the current working directory. Just enter the pwd command in the terminal to display the full path of the current working directory. Here is an example: $pw

In golang, regular expressions are a very powerful tool that can be used to match and verify various types of data, including file paths. In this article, we will explore how to use regular expressions to verify the legitimacy of file paths. The format of the file path varies between different operating systems. For example, in Windows operating systems, the file path usually takes the following format: C:UsersUserNameDesktopile.txt while in Linux and UNIX systems, it uses
