PHP file directory basic Operations _php tutorial

Source: Internet
Author: User
Tags fread rewind

PHP's file directory base operation


We know that the temporary declaration of the variable is stored in memory, even if it is a static variable, the script will be released after the run, so, want to persist the contents of a variable, one way is to write to the file, put on the hard disk or server, for this file operation must be familiar. 1. File property Information Gets the first file has the type, under Linux, there are blocks (block devices, such as disk partitions, CD-ROM), char (device with character input, such as keyboard, printer), dir (directory type, directory is also one of the files), FIFO (named pipe, Explanation is to transfer information from one process to another), file (normal file), link (link, like win-bottom shortcut), Unknow (unknown type) 7 categories, under Win, there are only 3 categories: file, dir, and unknown. Linux Slag said must be a good linux-_-, people are completely for Linux and born. There are several functions for the acquisition of types: filetype: Get type; Is_file: Determine if it is a normal file; Is_link: Determine if it is a link. There are several functions for obtaining properties: file_exists: Determine whether a file or directory exists, FileSize: Get file Size, is_readable, is_writable, is_executable: Readable, writable, executable; Filectime, Filemtime, fileatime: Get the file creation time (create), modify time (modify), Access time (access), return time stamp; stat: Get some basic information about the file, Returns an index associated with a mixed array. For example, you can tell the file type: Copy code = POW (2, +)) {return round ($size/pow (2, 40), 2). ' TB '; } else if ($size >= pow (2)) {return round ($size/pow (2, 30), 2). ' GB '; } else if ($size >= pow (2)) {return round ($size/pow (2, 20), 2). ' MB '; } else if ($size >= pow (2, ten)) {return round ($size/pow (2, 10), 2). ' KB '; } else{return Round ($size, 2). ' Byte '; The copy code is now integrated to get the file information, the code is as follows: Copy code '; Return if (Is_file ($path)) {//is a file that prints the base file name of Echo basename ($path). ' is a a file
'; if (Is_dir ($path)) {//is the directory, return to the directory echo dirname ($path). ' is a directory
'; } echo ' File type: '. Getfiletype ($path). '
'; Gets the file type echo ' File size: '. GetSize ($path). '
'; Gets the file size if (is_readable ($path)) {//is readable by the Echo basename ($path). ' Is readable
'; if (is_writeable ($path)) {//Can write echo basename ($path). ' Is writeable
'; if (is_executable ($path)) {//Can perform echo basename ($path). ' Is executable
'; The//Touch function can modify these times for Echo ' file Create Time: '. Date (' y-m-d h:i:s ', Filectime ($path)). '
'; Create time Echo ' file modify: '. Date (' y-m-d h:i:s ', Filemtime ($path)). '
'; Modified time echo ' last access: '. Date (' y-m-d h:i:s ', Fileatime ($path)). '
'; Last access time Echo ' file owner: '. Fileowner ($path). '
'; File owner echo ' file permission: '. substr (sprintf ('%o ', (Fileperms ($path))),-4). '
'; File permissions, octal output echo ' file group: '. Filegroup ($path). '
'; The group where the file is located} The copy code effect is as follows: The code also uses the file permissions, the group and other functions, it is necessary to explain (the wrong, please fix). The permissions of a file are divided into readable and writable executable, generally this means: rwx, the letter corresponding to the expression of the readable writable executable, the previous value is 4, 2, 1, three values add up to a maximum of 7, so 0666 with the octal representation, so it looks very convenient. 7 means the file has these three permissions, so why is 0666 printed? As we all know, under Windows There is a user, under Linux, similar to Windows, there is a user logged in, so a file may be owned by the user, a user it also has its own group, And there are other groups in the system (it is assumed that this should be a management need), so for 0666, for the first 6, the user's permissions on the file, and the second 6 indicates the user's group's permissions on the file. The third 6 represents the permissions that other groups have (so that you do not have to distinguish between other users in addition to this group), and 6 know that the file is readable and writable (the. exe file is known as executable under win). 2. Directory operation directory Read, Opendir: Open a directory, return a handle, point to the contents of the directory, if the contents of the directory as a sequential data, such as a sequential array, this handle points to the beginning of the array, in fact, The content in this directory is sorted by dictionary, whether it is a file or a subdirectory. Readdir: Reads the next directory content, returns the file name, and automatically points to the next file/directory in the directory, so reading the contents of a directory, not including the contents of subdirectories, need a loop to control, after reading, also close the handle variable, C language read the file is also the case, Turn it on and off. Example of my machine: copying code '; if (Is_dir ($dir)) {if ($handle = Opendir ($dir)) = = False) {//Get directory handle echo ' open dir failed '; return;} while (($name = Readd IR ($handle)) = False) {//loop reads the contents of the directory $filepath = $dir. '/'. $name; Echo ' name: '. $name. ' type: '. FileType ($filepath). '
'; } closedir ($handle); Close directory handle} else{echo $dir. ' is not a directory '; The copy code effect is as follows: You can see that in fact, the system gives the contents of the directory a sort of ignoring the case of the dictionary. The size of the directory is calculated, we know that the size of the file can be obtained by filesize, but there is no specific function in PHP to calculate the size of the directory. Of course, PHP has a function to calculate the size of the hard disk disk_total_space (calculate the total disk space), Disk_free_space (Calculate the hard disk free space), but I tried the next disk_free_space, seemingly incorrect calculation. Because there is filesize to calculate the size of the file, so you need to use recursion, when it is a directory, go in to continue to calculate the size of the subdirectory, if it is a file, get the file size and add the return, the code is as follows: Copy code
'; Call the previous data processing function to copy the code I built a size file in the F-disk, randomly made a list of ideas and documents, the effect is as follows, the left is the program, the right side is right-click View folder properties, to compare. New and deleted directories, mainly used, mkdir: Create a new directory, rmdir: Delete a non-empty directory, note can only be non-empty, the code is as follows: Copy code '; } else{if (File_exists ($newDirPath)) echo ' directory '. $newDirPath. ' has existed
'; else echo ' Create directory '. $newDirPath. ' Failed
'; if (true = = @rmdir (' f:/aaa '))//can only delete non-empty directories if the delete nonexistent directory automatically throws warning Echo ' remove successfully
Copy the code so the problem, if you want to delete a non-empty directory to do, and have to write their own, the idea is still recursive, because PHP only provides the deletion of the file function unlink, so in the deletion of a directory, the first opendir, then enter, if the file is deleted directly, if it is a directory, Continue to work with this method, and of course you can return a bool variable to indicate whether the deletion was successful, and the code is as follows: Copy code '; }}} Copy code if you want to read more flexible, with fseek, rewind use, they can move the file pointer to a specific location, fseek is very flexible, you can move directly to the beginning or the end, or move forward or backward from the current position, read the desired content, Ftell can also tell the current location , such as: Copy code '; The current file pointer position of the output file, in bytes, 0 for the beginning $str = Fread ($handle, 3); Reads 3 bytes While the pointer automatically moves back 3 bytes echo ' Read content: '. $str. '
'; Echo ' Current position: '. Ftell ($handle). '
'; Fseek ($handle, 5, seek_cur); Move the file pointer from the current position back 5 bytes echo ' position: '. Ftell ($handle). '
'; $str = Fread ($handle, 5); Echo ' Read content: '. $str. '
'; Echo ' Current position: '. Ftell ($handle). '
'; Rewind ($handle); Return the file at the beginning of echo ' current position: '. Ftell ($handle). '
'; Fseek ($handle, 0, seek_end); Move to the end of the file echo ' current position: '. Ftell ($handle). '
'; Fclose ($handle); Close File}}

http://www.bkjia.com/PHPjc/909451.html www.bkjia.com true http://www.bkjia.com/PHPjc/909451.html techarticle PHP file directory based operations we know that the temporary declaration of the variable is stored in memory, even if the static variable, the script will be released after the run, so, want to save long ...

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