Introduction to php folder and file directory operation functions_php basics
php folder operation function
string basename (string path [, string suffix])
Given a string containing the full path to a file, this function returns the base file name. If the filename ends with suffix, this part will also be removed.
In Windows, both slash (/) and backslash () can be used as directory separators. In other circumstances it is a slash (/).
string dirname (string path)
Given a string containing the full path to a file, this function returns the directory name after removing the file name.
In Windows, both slash (/) and backslash () can be used as directory separators. In other circumstances it is a slash (/).
array pathinfo ( string path [, int options] )
pathinfo() returns an associative array containing path information. Includes the following array elements: dirname, basename, and extension.
You can specify which units to return through the options parameter. They include: PATHINFO_DIRNAME, PATHINFO_BASENAME and PATHINFO_EXTENSION. The default is to return all units.
string realpath ( string path )
realpath() expands all symbolic links and handles '/./', '/../' and redundant '/' in the input path and returns the normalized absolute path name. There are no symbolic links, '/./' or '/../' components in the returned path.
realpath() returns FALSE if it fails, for example if the file does not exist. On BSD systems, if path simply does not exist, PHP will not return FALSE like other systems.
bool is_dir ( string filename )
Returns TRUE if the filename exists and is a directory. If filename is a relative path, its relative path is checked against the current working directory.
Note: The results of this function will be cached. See clearstatcache() for more information.
resource opendir ( string path [, resource context] )
Opens a directory handle that can be used in subsequent closedir(), readdir() and rewinddir() calls.
string readdir (resource dir_handle)
Returns the file name of the next file in the directory. File names are returned in order in the file system.
void closedir (resource dir_handle)
Close the directory stream specified by dir_handle. The stream must have been previously opened by opendir().
void rewinddir ( resource dir_handle )
Resets the directory stream specified by dir_handle to the beginning of the directory.
array glob ( string pattern [, int flags] )
Theglob() function finds all file paths that match pattern according to the rules used by the libc glob() function, similar to the rules used by ordinary shells. No abbreviation expansion or parameter substitution is performed.
Returns an array containing matching files/directories. Returns FALSE if an error occurs.
Valid tags are:
GLOB_MARK - Add a slash to each returned item
GLOB_NOSORT - Return the files in their original order of appearance in the directory (no sorting)
GLOB_NOCHECK - If there are no files Match returns the pattern used for the search
GLOB_NOESCAPE - backslash unescaped metacharacter
GLOB_BRACE - expands {a,b,c} to match 'a', 'b' or 'c'
GLOB_ONLYDIR - Returns only directory entries that match the pattern
Note: Prior to PHP 4.3.3, GLOB_ONLYDIR was not available on Windows or other systems that do not use the GNU C library.
GLOB_ERR - Stop and read error messages (such as unreadable directories), ignore all errors by default
Note: GLOB_ERR was added in PHP 5.1.
php file directory operations
Create a new file
1. First determine the content to be written into the file
$content = 'Hello';
2. Open this file (the system will automatically create this empty file)
/ / Assume that the newly created file is called file.txt and is in the upper-level directory. w means 'write file', which is used below $fp to point to an open file.
$fp = fopen('../file.txt', 'w');
3. Write the content string to the file
//$fp tells the system the file to be written, write The entered content is $content
fwrite($fp, $content);
4. Close the file
fclose($fp);
Note: PHP5 provides a more convenient function file_put_contents, above The 4 steps can be completed like this:
$content = 'Hello';
file_put_contents('file.txt',$content);
Delete files
//Delete the file abc.txt in the arch directory in the current directory
unlink('arch/abc.txt');
Note: The system will return the operation result, if successful Returns TRUE, otherwise returns FALSE. You can use a variable to receive it to know whether the deletion is successful:
$deleteResult = unlink('arch/abc.txt');
Get file content
//Assume that the target file name is file.txt, and it is in the upper-level directory. The obtained content is put into $content.
$content = file_get_contents('../file.txt');
Modify file content
The operation method is basically the same as creating new content
Rename file or directory
//Rename file 1.gif under subdirectory a in the current directory to 2.gif.
rename('/a/1.gif', '/a/2.gif');
Note: The same is true for directories. The system will return the operation result, TRUE if successful, and FALSE if failed. You can use a variable to receive it to know whether the rename is successful.
$renameResult = rename('/a/1.gif', '/a/2.gif');
If you want to move a file or directory, just set the renamed path to the new path. That’s it:
//Move the file 1.gif under subdirectory a in the current directory to subdirectory b under the current directory, and rename it to 2.gif.
rename('/a/1.gif', '/b/2.gif');
However, please note that if directory b does not exist, the move will fail.
Copy file
//Copy the file 1.gif in subdirectory a in the current directory to subdirectory b in the current directory, and name it 2.gif.
copy('/a/1.gif', '/b/1.gif');
Explanation: This operation cannot be performed on the directory.
If the target file (/b/1.gif above) already exists, the original file will be overwritten.
The system will return the operation result, TRUE if successful, and FALSE if failed. You can use a variable to receive it to know whether the copy was successful.
$copyResult = copy('/a/1.gif', '/b/1.gif');
Moving files or directories
The operation method is the same as renaming
Whether the file or directory exists
//Check whether the file logo.jpg in the upper-level directory exists.
$existResult = file_exists('../logo.jpg');
Note: The system returns true if the file exists, otherwise it returns false. The same operation can be done with directories.
Get the file size
//Get the size of the file logo.png in the upper directory.
$size = filesize('../logo.png');
Explanation: The system will return a number indicating the size of the file in bytes.
Create a new directory
//Create a new directory b below directory a in the current directory.
mkdir('/a/b');
Note: The system will return the operation result. TRUE if successful, FALSE if failed. You can use a variable to receive it to know whether the new creation is successful:
$mkResult = mkdir('/a/b');
Delete Directory
//Delete subdirectory b below directory a in the current directory.
rmdir('/a/b');
Note: Only non-empty directories can be deleted, otherwise the subdirectories and files under the directory must be deleted first, and then the total directory
The system will return the operation results , returns TRUE if successful, and FALSE if failed. You can use a variable to receive it to know whether the deletion is successful:
$deleteResult = rmdir('/a/b');
Get all file names in the directory
1. First open the directory to be operated and use a variable to point to it
//Open the subdirectory common under the directory pic in the current directory.
$handler = opendir('pic/common');
2. Loop to read all files in the directory
/*where $filename = readdir($handler) is the The read file name is assigned to $filename. In order not to get stuck in an infinite loop, $filename !== false is also required. Be sure to use !==, because if a file name is called '0', or something is considered by the system to represent false, using != will stop the loop */
while( ($filename = readdir($ handler)) !== false ) {
3. There will be two files in the directory, named '.' and '..', do not operate them
if($filename != "." && $filename != "..") {
4. Process
//Here we simply use echo to output the file name
echo $filename;
}
}
5. Close the directory
closedir($handler);
Whether the object is a directory
//Check whether the target object logo.jpg in the upper-level directory is a directory.
$checkResult = is_dir('../logo.jpg');
Note: If the target object is a directory system, return true, otherwise return false. Of course $checkResult in the above example is false.
Whether the object is a file
//Check whether the target object logo.jpg in the upper-level directory is a file.
$checkResult = is_file('../logo.jpg');
Note: If the target object is a file, the system returns true, otherwise it returns false. Of course $checkResult in the above example is true.

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

AI Hentai Generator
Generate AI Hentai for free.

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



By default, Windows 11 does not display folder sizes in File Explorer, however, you can make certain changes in Explorer settings to make them visible. In this guide, we will discuss some of the easiest ways to display folder sizes so that you can effectively manage disk space on your PC. How to check the size of a folder on Windows 11? 1. Use the folder's Properties window to open a Windows Explorer window by pressing +. WindowsE Go to the folder whose size you want to check, right-click on it and select "Properties" from the context menu. In the folder properties window, go to the "General" tab and find the "Size" section to find out how much space the folder takes up. 2. Enable "

The win11 system has updated a lot of new wallpapers for everyone, so many users are curious about which folder the win11 wallpapers are in and want to open them to see the wallpapers inside. Therefore, we have brought a tutorial so that you can enter and view the wallpapers. . Which folder is the win11 wallpaper in: 1. The system comes with wallpaper: 1. First enter my computer, and then open the path: C:\Windows\Web\Wallpaper. 2. Then enter the windows folder and you can see the wallpapers that come with the system. 2. User-saved wallpapers: 1. Wallpapers installed by users will be saved in: C:\Users (user)\xx (current user name)\AppData\Local\Microso

In this article, we will show you how to automatically copy files to another folder on Windows 11/10. Creating backups is necessary to avoid data loss. Data loss can occur due to many reasons such as hard drive corruption, malware attack, etc. You can back up your data manually by using copy and paste method or using third-party tools. Did you know you can automatically back up data on your Windows computer? We'll show you how to do this in this article. How to have files automatically copied to another folder on Windows 11/10 How to use Task Scheduler to automatically copy files and folders to another destination folder on Windows 11/10? This article will provide you with detailed guidance. please

Recently, many friends feel that the theme of Win10 does not meet their own aesthetics and want to change the theme. After downloading it online, they find that the folder cannot be found. Then the editor will show you how to find the folder of the Win10 theme. Which folder is the win10 theme in? 1. The default storage path location of Win10 system wallpapers: 1. Microsoft saves these pictures in the path C:\Windows\Web\Wallpaper. Under it are the default saves of pictures with three different themes. Location, 2, flowers and lines and colors theme pictures are also saved in the folder with the same name! The naming principle is imgXXX. We only need to follow this principle to change the name of the related image we want to set and paste the image into

When you find that one or more items in your sync folder do not match the error message in Outlook, it may be because you updated or canceled meeting items. In this case, you will see an error message saying that your local version of the data conflicts with the remote copy. This situation usually happens in Outlook desktop application. One or more items in the folder you synced do not match. To resolve the conflict, open the projects and try the operation again. Fix One or more items in synced folders do not match Outlook error In Outlook desktop version, you may encounter issues when local calendar items conflict with the server copy. Fortunately, though, there are some simple ways to help

After updating to the latest win11 system, most friends don't know how to encrypt their folders to protect privacy, so we have brought you a method. Let's take a look at how to set a password for a win11 computer folder. How to set a password for a win11 computer folder: 1. First find the folder you want to encrypt. 2. Then right-click the folder and select "Properties". 3. Click "Advanced" under Properties. 4. Check "Encrypt content to protect data" in the menu and click OK. 5. Finally, return to the properties of the folder and click "OK" in the pop-up window.

Many users change wallpapers when using their computers. I believe many users are also asking which folder the win11 wallpapers are in? The wallpapers that come with the system are in Wallpaper under the C drive, and the wallpapers saved by users are in the Themes folder of the C drive. Let this site carefully introduce the win11 default wallpaper path sharing for users. Share win11 default wallpaper path 1. The system comes with wallpaper: 1. First enter my computer, and then open the path: C: Windows Web Wallpaper. 2. User-saved wallpapers: 1. Wallpapers installed by users will be saved in: C: Users (user) xx (current user name) AppDataLocalM

The Windows folder contains the Windows operating system and is an important folder in a Windows computer. By default, Windows is installed on the C drive. Therefore, C is the default directory for Windows folders. Every Windows computer has a Windows folder. However, some users reported that two Windows folders were found in the C drive. In this article, we will explain what you can do if you encounter such a situation. Two Windows folders in C drive It is rare to have two Windows folders in C drive. However, if you encounter such a situation, you can use the following suggestions: Run an anti-malware scan to try to find the correct
