Basic operations for directories and files one (44)

Source: Internet
Author: User
Tags fread readfile phpmyadmin
Parse directory path

PHP scripts make it easy to manipulate directories, such as creating directories, traversing directories, and directories and deleting directories.
?? Common file directory path format:
?? $unixPath = "/var/www/html/index.php";
Absolute paths in Unix systems must be delimited with "/"
?? $winPath = "c:\\appserv\\www\\index.php";
In the absolute path of the Windows system, the default use of "\" separates
?? $winPath 2= "c:/appserv/www/index.php";
You can also use the "/" separator in Windows systems.
?? Note Use absolute and relative paths.

PHP File path correlation function

?? basename--returns the file name portion of the path

?? Syntax: string basename (String path [, string suffix])
?? Give a string containing a full path to a file, this function returns the base file name. If the file name ends in suffix, then this part will be removed as well.
?? dirname--returns the directory portion of the path
?? Syntax: string dirname (String path)
?? Gives a string containing a full path to a file, which returns the directory name after removing the file name.
$path = "/home/httpd/html/index.php";
$file = basename ($path);//$file Value: "index.php"
$file = basename ($path, ". php"); $file value: "Index"
$file = DirName ($path); $file value: "/home/httpd/html"
?>

pathinfo--return the file path information
?? Syntax: array pathinfo (string path [, Intoptions])
?? PathInfo () returns a federated array containing information about path. Includes the following array cells: dirname,basename and extension.
$path _parts= pathinfo ("/www/htdocs/index.html");
echo $path _parts["DirName"]. "\ n"; /www/htdocs
echo $path _parts["basename"]. "\ n"; Index.html
echo $path _parts["extension"]. "\ n"; Html
?>
?? realpath--returns the normalized absolute path name
?? Syntax: string realpath (String path)
?? Realpath () expands all the symbolic connections and processes the '/./' in the input path, '/'. /' and extra '/' and returns the normalized absolute path name. The path returned does not have a symbolic connection, '/./' or '/'. /' ingredient.

Traverse Directory

opendir--opening a directory handle
?? Syntax: Resource opendir (string path [, resource context])
?? Opens a directory handle that can be used later in the Closedir (), Readdir (), and Rewinddir () calls.
?? readdir--reading entries from the directory handle
?? Syntax: String readdir (Resource Dir_handle)
?? Returns the file name of the current directory pointer position, does not return false, and moves the pointer down one bit. The file name is returned as a sort in the file system.
?? closedir--Closing a directory handle
?? Syntax: void Closedir (Resource dir_handle)
?? Closes the directory stream specified by Dir_handle. The stream must be opened previously by Opendir ().
?? rewinddir--reverse the directory handle
?? Syntax: void Rewinddir (Resource dir_handle)
?? Resets the directory stream specified by Dir_handle to the beginning of the directory.

Statistics Directory Size

?? disk_free_space--returns the free space in the directory
?? Syntax: float disk_free_space (string directory)
?? Given a string containing a directory, this function returns the number of available bytes based on the corresponding file system or disk partition.
?? disk_total_space--returns the total disk size of a directory
?? Syntax: float disk_total_space (string directory)
?? Given a string containing a directory, this function will return all bytes based on the corresponding file system or disk partition.

Example

 //Customize a function dirsize () to count the directory size of incoming parametersfunction Dirsize ($directory) {$dir _size=0;//The initial value is 0, which is used to accumulate each file size to calculate the directory sizeif($dir _handle= @opendir ($directory)) {//Open Directory and decide to open it successfully while($filename =readdir ($dir _handle)) {//Looping through Directoriesif($filename! ="."&& $filename! ="..") {//exclude a special directory$subFile = $directory."/". $filename;//Connecting files and directoriesif(Is_dir ($subFile))//If the directory$dir _size+=dirsize ($subFile);//to find the size of subdirectoriesif(Is_file ($subFile))//if the file$dir _size+=filesize ($subFile);//find the size of the file and accumulate it}}closedir ($dir _handle); //Close File Resourcereturn$dir _size;//returns the calculated directory size}} $dir _size=dirsize ("PhpMyAdmin");//The function calculates the directory size, returns the directory sizeEcho Round ($dir _size/pow (1024x768,1),2)."KB";//Swap Directory bytes for "KB" units?>

Create and delete directories

mkdir--New Directory
?? Syntax: Boolmkdir (String pathname [, Intmode])
?? Try creating a new directory that is specified by pathname.
?? rmdir--Deleting a directory
?? Syntax: Boolrmdir (String dirname)
?? An attempt was made to delete the directory specified by DirName. The directory must be empty and have the appropriate permissions. Returns true if successful, and false if it fails.
?? unlink--Deleting files
?? Syntax: Boolunlink (string filename)
?? Delete filename. Similar to the unlink () function of Unix C. Returns True if successful, false if unsuccessful

 //the Custom function recursively deletes the entire directoryfunction Deldir ($directory) {if(File_exists ($directory)) {//determines whether the directory exists and executes if it existsif($dir _handle= @opendir ($directory)) {//Open the Return directory resource and determine while($filename =readdir ($dir _handle)) {//Traverse Directory to read the information in the directoryif($filename! ="."&& $filename! ="..") {//Be sure to exclude two special directories$subFile = $directory."/". $filename;//connect the files in the directory to the current directoryif(Is_dir ($subFile))//If the directory condition is setDeldir ($subFile);//recursively call yourself to delete a subdirectoryif(Is_file ($subFile))//If the file condition is setUnlink ($subFile);//Delete this file directly}}closedir ($dir _handle); //Close Directory ResourceRmDir ($directory);//Delete Empty directory}}}deldir ("PhpMyAdmin");//Call the function to delete the phpMyAdmin folder in the directory where the program is located?>

Copying and moving directories

copy--Copy Files
?? Syntax: boolcopy (string source, String dest)
?? Copy the file from source to Dest. Returns true if successful, and false if it fails.
?? There are no related functions for copying and moving directories in PHP. As needed, as long as the custom function.

Basic operation of the file

Opening and closing of files

fopen--Open file or URL
?? Syntax: Resource fopen (string filename, string mode [, booluse_include_path[, Resource Zcontext]])
?? fopen () binds the name resource specified by filename to a stream. If filename is "scheme://..." Format, the Search protocol processor (also known as the Encapsulation Protocol) is treated as a url,php to handle this pattern. If the protocol has not yet been registered for encapsulation, PHP will issue a message to help examine potential problems in the script and continue with filename as a normal file name.
?? The mode parameter specifies the type of access required to the stream.
?? If you also need to search for files in include_path, you can set the optional third parameter use_include_path to ' 1 ' or true.
?? If open fails, this function returns FALSE.
fclose--Close an open file pointer

Write file

fwrite--writing files (can be used safely in binary files)
?? Syntax: Intfwrite (resource handle, String string[, Intlength])
?? Fwrite () writes the contents of the string to the file pointer handle. If length is specified, the write stops when the length byte is written or after the string is written, depending on which situation is encountered first. Returns the number of characters written, or False if an error occurs

Read File contents

?? fread--read files (can be used safely in binary files)
?? String Fread (Inthandle, Intlength)
?? Fread () reads up to length bytes from a file pointer handle. The function will stop reading a file when it is available, or (for a network stream) when it has been read by the length of bytes, or when it reaches EOF, depending on what kind of situation is encountered first.

 
  Php$handle = fopen ("http://www.example.com/""RB"  "" ;  while (! feof ($handle)) {$contents. 8192 );} Fclose ($handle);? >

fgets--reading a line from the file pointer
?? Syntax: string fgets (Inthandle [, Intlength])
?? Reads a row from the file pointed to by handle and returns a string of up to length-1 bytes in length. Stop after encountering a newline character (included in the return value), EOF, or having read the length-1 byte (see first the case). If length is not specified, the default is 1 K, or 1024 bytes.
?? fgetc--reading characters from the file pointer
?? Syntax: string fgetc (Resource handle)
?? Returns a string containing a character that is obtained from the file that handle points to. Returns False if EOF is encountered.

file--to read an entire file into an array
?? Syntax: Array file (string filename [, intuse_include_path[, resource context])
?? As with ReadFile (), only file () is returned as an array. Each cell in the array is the corresponding line in the file, including the newline character. False if the failed file () is returned.
?? readfile--output a file
?? Syntax: intreadfile (string filename [, booluse_include_path[, resource context])
?? Reads a file and writes to the output buffer.
?? Returns the number of bytes read from the file. If an error returns false and the error message is displayed unless it is called in the form of @readfile ().

The above describes the directory and the basic operation of the file one (44), including the aspects of the content, I hope to be interested in the PHP tutorial friends helpful.

  • 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.