Summary of PHP file operations (read, write, delete)

Source: Internet
Author: User
Tags glob
Permission issue 07771 readable 2 writable 4 executable Super User common user other user

[File and directory operations]
1. directories and files (common files and executable files)
2. permission issue 0777 1 readable 2 writable 4 executable Super User other users
3. file operations can be understood as multi-file pointer operations.
[Directory operations]
1. create the directory mkdir () and return the bool parameter $ pathname $ mode $ recursive.
2. delete the rmdir () directory and return the bool parameter $ path.
3. rename the directory rename () and return the bool parameter $ oldname $ newname.
4. Read the Directory
A) open the directory handle opendir () and return the resource $ path.
B) read readdir () of the directory to return the name parameter of the directory $ resource. note: the built-in pointer is similar to the array pointer.
Scandir () returns an array parameter consisting of directories $ path $ order note: No built-in pointer
5. the closedir () parameter for directory closure $ handle ($ handle = opendir ())
6. reset the directory pointer rewinddir () parameter $ handle ($ handle = opendir ())
7. determine whether the is_dir () directory is used to return the bool parameter $ dirname
[Directory operation considerations]
1. the directory handle is a resource. close closedir ($ handle) after opening it)
2. the scandir () function operation is directly the path of the folder.
3. directory reading lists all the folders under it, including all files
4. rename and cut files and folders when using the rename () function
[Directory example]
Recursive directory reading
[File operations]
1. differences between common and executable files
2. create a file
3. open a file
Fopen (): returned resource $ filename $ mode true | false
$ Mode:
R: the read pointer points to the file header.
R +: the read/write pointer points to the file header.
W: The Write pointer points to the file header and the size is 0. no attempt is made.
W +: the read/write pointer points to the file header and the size is 0. no attempt is made.
A: The Write pointer pointing to the end of the file has not been created.
A +: no attempt to create a read/write pointer pointing to the end of the file
X: it is created and opened in write mode. If an error exists, it is not created.
X +: created and opened in read/write mode. If an error exists, it is not created.
In Windows, a text conversion mark ("t") is provided to transparently convert \ n to \ r \ n.
You can also use "B" to force the binary mode to avoid data conversion.
To use these tags, either use "B" or "t" as the last character of the mode parameter.
4. determine whether a file is used and related permissions.
Is_file () returns bool $ pathname
Is_readable () // if (&&)
Is_writeable ()
Is_executable () URL ('../')
5. file reading
1. fread () return value: string parameter $ fopen $ length
2. feof () checks whether bool $ fopen is returned after reading the object.
3. fgetc () reads a character from the file and returns the string parameter $ fopen
4. fgets () reads a row from the file and returns the string $ fopen $ length
5. fgetss () reads a row from the file pointer and filters out the HTML tag $ fopen $ length $ allowtag
6. file_get_contents () returns string $ path true | false
(Whether to search for files in the include folder set in the configuration file)
7. ftell () returns the pointer position $ fopen
Fseek () returns 0 |-1 parameter $ fopen $ offset $ position
$ Position: seek_set | seek_cur | seek_end
8. rewind () returns the bool reset file pointer parameter $ fopen
6. file writing
1. the fwrite () parameter returns the number of characters written to the file. $ fopen $ string $ length
2. fputs () fwrite alias
3. file_put_contents () $ filename $ string $ flags
$ Flag: FILE_USE_INCLUDE_PATH | FILE_APPEND | LOCK_EX
7. file attributes
Disk attribute: disk_free_space () | disk_total_space ()
File attributes: filesize () | fileatime () | filectime () | filemtime ()
Returns a unix timestamp int.
8. delete an object
Unlink () returns the bool parameter $ filename
[Output Buffer]
Output buffing
When ob_start is enabled, the output buffer encounters an end function.
Ob_end_clean: clears the Buffered content and ends the buffer.
Ob_end_flush outputs the buffer content and ends the buffer.
Ob_clean clears the buffer but does not end the buffer below
Ob_flush outputs the buffer but does not end the buffer below
Ob_get_contents obtains the Buffered content.
Ob_get_length: obtains the number of bytes in the buffer.
Ob_get_clean: get the current buffer content and clear the current buffer content
Ob_get_flush
[Other functions]
1. include () | include_once () page contains functions
2. The require () | require_once () page contains the configuration file.
3. the php. ini configuration file contains files.
Get_include_path (); returns a string
Set_include_path (); returns the string parameter $ string path
4. ini_set () | ini_get | get_var _var () | ini_restore () | ini_get_all () | get_loaded_extensions ()
5. convert \ n
Nl2br () returns the string parameter $ string $ is_xhtml (true | false)
6. glob () returns matched directories and files (arrays) $ pattern $ flag
$ Flag: GLOB_MARK | GLOB_NOSORT | GLOB_NOCHECK |
GLOB_NOESCAPE | GLOB_BRACE | GLOB_ONLYDIR GLOB_ERR |
7. pathinfo (); returns an array.
8. dirname () | basename () | getcwd () | chdir () |
[Example]
1. specify the folder and file name for file upload (precise to S)
2. recursive reading and deletion of directories and files
3. Recursive directory creation
[Note]
1. rename () | file_exists () | readdir () | scandir () can be used to operate both files and directories.
2. exclude '.' and '.' during directory reading '..'
3. when opening a file in write mode, the file will be cleared first.
5. to delete a folder, you must first delete the subfolders and files.
6. to include or open a remote file, you must set the php. ini configuration file.
Allow_url_fopen and allow_url_include
7. except file_get_contents () | scandir () | file_put_contents () |
Glob () is a function that operates on the handle.
8. remember to close the handle.
Close closedir () of directory handle no return value
Close fclose () of file handle returns bool
9. the handle is a resource type and cannot be serialized.


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.