Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the operation functions of php folder and file directory

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the "php folder and file directory operation functions", in the daily operation, I believe that many people in the php folder and file directory operation functions have doubts, Xiaobian consulted all kinds of information, sorted out a simple and easy to use method of operation, hope to answer the "php folder and file directory operation functions which" of the doubt is helpful. Next, please follow the editor to study!

Php folder operation function

String basename (string path [, string suffix])

Given a string that contains the full path to a file, this function returns the basic file name. If the file name ends with suffix, this part will also be removed.

In Windows, both the slash (/) and the backslash () can be used as directory separators. In other environments, it is a slash (/).

String dirname (string path)

Given a string that contains the full path to a file, this function returns the directory name after the file name is removed.

In Windows, both the slash (/) and the backslash () can be used as directory separators. In other environments, it is a slash (/).

Array pathinfo (string path [, int options])

Pathinfo () returns an associative array containing information about path. Includes the following array units: dirname,basename and extension.

You can specify which cells to return through the parameter options. They include: PATHINFO_DIRNAME,PATHINFO_BASENAME and PATHINFO_EXTENSION. The default is to return all cells.

String realpath (string path)

Realpath () extends all symbolic links and processes the'/. /','/.. / 'and redundant' /'in the input path and returns the normalized absolute pathname. There is no symbolic link,'/. /'or'/.. / 'component in the returned path.

Return FALSE when realpath () fails, such as if the file does not exist. On BSD systems, PHP does not return FALSE like other systems if only path does not exist.

Bool is_dir (string filename)

Returns TRUE if the file name exists and is a directory. If filename is a relative path, check its relative path against the current working directory.

Note: the results of this function are cached. See clearstatcache () for more information.

Resource opendir (string path [, resource context])

Open 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. The file name is returned as sorted in the file system.

Void closedir (resource dir_handle)

Closes the directory stream specified by dir_handle. The stream must be 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])

The glob () function looks for all file paths that match pattern according to the rules used by the libc glob () function, similar to the rules used by normal shells. There is no abbreviation extension or parameter substitution.

Returns an array containing matching files / directories. Return FALSE if there is an error.

Valid tags are:

GLOB_MARK-add a slash to each returned item

GLOB_NOSORT-returns in the original order in which the files appear in the directory (not sorted)

GLOB_NOCHECK-returns the mode used for the search if no files match

GLOB_NOESCAPE-backslash does not escape metacharacters

GLOB_BRACE-extend {arecrome bjorc} to match'axiajiaojia'or 'c'

GLOB_ONLYDIR-only directory entries that match the pattern are returned

Note: prior to PHP 4.3.3, GLOB_ONLYDIR is 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) and ignore all errors by default

Note: GLOB_ERR was added by PHP 5.1.

Php file directory operation

Create a new file

1. Determine what you want to write to the file first.

$content = 'Hello'

2. Open the file (the system will automatically create the empty file)

/ / assume that the newly created file is called file.txt and is in the parent directory. W stands for 'write file', which is used under $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 write, and the content to write is $content

Fwrite ($fp, $content)

4. Close the file

Fclose ($fp)

Note: a more convenient function file_put_contents is provided in PHP5. The above four steps can be completed as follows:

$content = 'Hello'

File_put_contents ('file.txt',$content)

Delete a file

/ / Delete the file abc.txt in the arch directory under the current directory

Unlink ('arch/abc.txt')

Note: the system will return the operation result, TRUE for success, and FALSE for failure. You can receive it with a variable to know whether the deletion is successful:

$deleteResult = unlink ('arch/abc.txt')

Get the contents of the file

/ / assume that the target file name obtained is file.txt and is in the parent directory. The acquired content is put into $content.

$content = file_get_contents ('.. / file.txt')

Modify the contents of the file

The operation method is basically the same as the new content.

Rename a file or directory

/ / rename the file 1.gif under subdirectory a under the current directory to 2.gif.

Rename ('/ a * 1.gif * *,'/ *

Description: the same is true for directories. The system returns the operation result, TRUE for success and FALSE for failure. You can receive it with a variable to know whether the rename is successful or not.

$renameResult = rename ('/ a _

If you want to move a file or directory, simply set the renamed path to the new path:

/ / move the file 1.gif under subdirectory a under the current directory to subdirectory b under the current directory and rename it to 2.gif.

Rename ('/ a * 1.gif * *,'/ b * 2.gif')

Note, however, that if directory b does not exist, the move will fail.

Copy a file

/ / copy the file 1.gif under subdirectory a under the current directory to subdirectory b under the current directory and name it 2.gif.

Copy ('/ a * 1.gif * *,'/ b * 1.gif')

Description: this operation cannot be performed on a 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 for success and FALSE for failure. You can receive it with a variable to know whether the copy is successful or not.

$copyResult = copy ('/ a _ 1.gif _,'/ _

Move a file or directory

Operate in the same way as renaming

Whether a file or directory exists

/ / check whether the file logo.jpg in the parent directory exists.

$existResult = file_exists ('.. / logo.jpg')

Note: if the file exists, the system returns true, otherwise returns false. You can do the same for directories.

Get file size

/ / get the size of the file logo.png in the parent directory.

$size = filesize ('.. / logo.png')

Description: the system will return a number indicating how many bytes the file size is (bytes).

Create a new directory

/ / create a new directory b under directory a under the current directory.

Mkdir ('/ a _ b')

Note: the system will return the operation result, TRUE for success, and FALSE for failure. You can receive it with a variable to know whether the new project is successful:

$mkResult = mkdir ('/ a Compact b')

Delete directory

/ / Delete subdirectory b under directory a under 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 must be deleted

The system returns the operation result, TRUE for success, and FALSE for failure. You can receive it with a variable to know whether the deletion is successful:

$deleteResult = rmdir ('/ a Compact b')

Get all file names in the directory

1. First open the directory you want to operate and point to it with a variable

/ / Open the subdirectory common under the current directory pic.

$handler = opendir ('pic/common')

2. Read all the files in the directory in a loop

/ * where $filename = readdir ($handler) assigns the read file name to $filename for each loop, so let $filename! = = false in order not to fall into an endless loop. Be sure to use! =, because if a file name is called '0upload, or if some file name is considered to represent false by the system, using! = will stop the loop * /

While (($filename = readdir ($handler))! = = false) {

3. There will be two files under the directory, named'.' And '...', don't operate on them.

If ($filename! = "." & & $filename! = "..") {

4. Deal with it

/ / here 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 under the parent directory is a directory.

$checkResult = is_dir ('.. / logo.jpg')

Note: return true if the target object is a directory system, otherwise return false. The $checkResult in the above example is of course false.

Whether the object is a file

/ / check whether the target object logo.jpg in the parent 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. The $checkResult in the above example is of course true.

At this point, on the "php folder and file directory what are the operation functions" on the end of the study, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report