PHP folder/File directory action function

Source: Internet
Author: User
Tags file size glob mkdir parent directory php file

PHP Folder action function


String basename (String path [, string suffix])

Gives a string containing a full path to a file, which returns the base file name. If the filename ends with suffix, that part will also be removed.

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

String dirname (String path)

Gives a string containing a full path to a file, which returns the name of the directory after the file name is removed.

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

Array pathinfo (string path [, int options])

PathInfo () returns a federated array containing the information with path. Includes the following array cells: 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 the cells.

String Realpath (String path)

Realpath () Expands all symbolic connections and processes the '/./' in the input path, '/. /' and superfluous '/' and return normalized absolute pathname. There is no symbolic connection in the returned path, '/./' or '/... /' ingredient.

Returns FALSE when Realpath () fails, such as if the file does not exist. On the BSD system, if only path does not exist, PHP does not return FALSE as other systems do.


BOOL Is_dir (string filename)

Returns TRUE if the filename exists and is a directory. If filename is a relative path, check its relative path according to the current working directory.

Note: The result 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. The file name is returned in the sort in the file system.


void Closedir (Resource dir_handle)

Closes the directory stream specified by Dir_handle. The stream must be opened before Opendir ().


void Rewinddir (Resource dir_handle)

Resets the directory flow specified by Dir_handle to the beginning of the directory.


Array Glob (string pattern [, int flags])

The Glob () function finds all file paths that match pattern with the rules used by the libc glob () function, similar to the rules used in general shells. No abbreviation extension or parameter substitution.

Returns an array that contains a matching file/directory. Returns FALSE if an error occurs.

Valid marks are:

Glob_mark-Add a slash to each returned item
Glob_nosort-Returns (not sorted) according to the original order in which the files appear in the directory
Glob_nocheck-Returns the pattern for searching if no file match
Glob_noescape-Backslash does not escape meta characters
Glob_brace-expand {a,b,c} to match ' A ', ' B ' or ' C '
Glob_onlydir-Returns only directory entries that match the pattern

Note: prior to PHP version 4.3.3, Glob_onlydir is not available on Windows or other systems that do not use the GNU C library.

Glob_err-Stops and reads error messages (such as unreadable directories), ignoring all errors by default

Note: Glob_err is added by PHP 5.1.

PHP file Directory operation


New file

1, first determine the content to write the file

$content = ' Hello ';

2, open this file (the system will automatically create this empty text)

Suppose the new file is called File.txt and is in the parent directory. W means ' Write file ', $fp to use, to point to an open file.

$fp = fopen ('.. /file.txt ', ' W ');

3. Write the content string to the file

$FP tell the system to write to the file, write the content is $content

Fwrite ($fp, $content);

4, close the file

Fclose ($FP);

Description: PHP5 provides more convenient function file_put_contents, the above 4 steps can be done this way:

$content = ' Hello ';

File_put_contents (' file.txt ', $content);

deleting files

Deletes files in the current directory under the Arch directory Abc.txt

Unlink (' Arch/abc.txt ');

Description: The system returns the operation result, the success returns TRUE, the failure returns FALSE, can receive with the variable, knows whether deletes succeeds:

$deleteResult = unlink (' Arch/abc.txt ');

Get file Contents

Assume that the target filename obtained is file.txt and is in the parent directory. Gets the content into the $content.

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

Modify the contents of a file

Action method is basically the same as new content

Renaming files or directories

Rename the file 1.gif below the subdirectory a in the current directory to 2.gif.

Rename ('/a/1.gif ', '/a/2.gif ');

Description: Same for catalogs. The system returns the result of the operation, returns TRUE for success, returns FALSE if it fails, and can receive with a variable to know whether to rename successfully.

$renameResult = Rename ('/a/1.gif ', '/a/2.gif ');

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

Move the file 1.gif under subdirectory A in the current directory to sub directory B in the current directory and rename to 2.gif.

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

Note, however, that if directory B does not exist, it will fail to move.

Copying files

Copy the file 1.gif under subdirectory A in the current directory to sub directory B in the current directory and name 2.gif.

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

Description: You cannot do this for a directory.

If the target file (/b/1.gif above) already exists, the original file will be overwritten.

The system returns the result of the operation, returns TRUE if it succeeds, returns FALSE if it fails, and can receive with the variable to know if replication succeeds.

$copyResult = Copy ('/a/1.gif ', '/b/1.gif ');

Move a file or directory

Action method is the same as renaming

Whether a file or directory exists

Check to see if the file logo.jpg exists in the parent directory.

$existResult = File_exists ('.. /logo.jpg ');

Description: Returns False if the file exists and the system returns TRUE. You can do the same thing with the directory.

Get File size

Gets the size of the file Logo.png in the parent directory.

$size = FileSize ('.. /logo.png ');

Description: The system returns a number that indicates how many bytes (bytes) The file is in size.

New directory

Create a new directory B under directory A in the current directory.

mkdir ('/a/b ');

Description: The system returns the operation result, succeeds returns TRUE, the failure returns FALSE, can receive with the variable, knew whether the new success:

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

Delete Directory

Deletes the subdirectory b under directory A in the current directory.

RmDir ('/a/b ');

Note: You can delete only non-empty directories, or you must first delete subdirectories and files under the directory, and then delete the total directory

The system returns the result of the operation, returns TRUE for success, returns FALSE if it fails, and can receive with the variable to know if the deletion succeeded:

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

Get all file names in the directory

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

Open the subdirectory under PIC under the current directory common.

$handler = Opendir (' Pic/common ');

2. Read all files in the directory under the loop

/* Where $filename = Readdir ($handler) assigns the read file name to the $filename each time the loop is cycled, so that $filename!== false in order not to be trapped in a dead loop. Be sure to use!==, because if a filename is called ' 0 ', or if some of the system is considered to be false,!= will stop the loop.

while (($filename = Readdir ($handler))!== false) {

3, the directory will have two files, the name is '. ' and '.. ', do not operate on them

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

4, to deal with

Here it simply uses echo to output the file name

echo $filename;

}

}

5, close the directory

Closedir ($handler);

Whether the object is a directory

Check to see if the target object logo.jpg in the parent directory is a directory.

$checkResult = Is_dir ('.. /logo.jpg ');

Description: Returns False if the target object is the directory system returns TRUE. The $checkresult of the above example is of course false.

Whether the object is a file

Check whether the target object logo.jpg is a file in the parent directory.

$checkResult = Is_file ('.. /logo.jpg ');

Note: If the target object is a file, the system returns TRUE, otherwise it returns false. The $checkresult of the above example is of course true.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.