PHP brief introduction to file operations

Source: Internet
Author: User
File operations have always been a headache for Web programmers, and file operations are necessary in systems like CMS. Nowadays, the function content for PHP file operations is very powerful, and this part of the file is also an important part of learning PHP. I hope you will not ignore it. This article will briefly introduce several basic file operations in PHP, with the PHP file Function

File operations have always been a headache for Web programmers, and file operations are necessary in systems like CMS. Nowadays, the function content for PHP file operations is very powerful, and this part of the file is also an important part of learning PHP. I hope you will not ignore it. This article will briefly introduce several basic file operations in PHP, with the PHP file Function

File operations have always been a headache for Web programmers, and file operations are necessary in systems like CMS. Nowadays, the function content for PHP file operations is very powerful, and this part of the file is also an important part of learning PHP. I hope you will not ignore it. This article will briefly introduce several basic file operations in PHP, with a summary of PHP file functions for your reference and learning.

1. open the file: fopen ("file name", "Mode ");

Mode parameters:

R: Read-Only. Point the file pointer to the file header. R +: read/write, pointing the file pointer to the file header. W: Write-only. open and clear the file content. If the file does not exist, create the file. W +: read/write. open and clear the file content. If the file does not exist, create the file. A: append, open, and write to the end of the file. If the file does not exist, a new file is created. A +: Read/append. The file content is maintained by writing content to the end of the file.

2. close the file: fclose ();
After using the file, you must explicitly tell PHP that the file has been used. For example:

$ File = fopen ("test.txt", "r"); // close an opened file pointer // some code be executed fclose = ($ file );

3. Check whether the file end is reached: feof ();

Example: if (feof ($ file) {echo "end of file ";}

4. Read files row by row: fgets ();
For example:

$file=fopen("test.txt","r");  while(!feof($file)){  echo fgets($file)."< br/>";  }  fclose($file); 

5. Read files by character: fgetc ()

6. Read any binary data: fread ()

7. Determine the File Reading status
Each file handle has a file pointer. According to the mode parameter of the fopen function, the file pointer is initially at the beginning of the file or at the end of the file. Feof () can be used to determine whether the file has reached the end. The filesize () function returns the file size.
8. File writing and permission judgment

Fwrite () function execution file write is_readable () // determine whether the file is readable is_writeable () // determine whether the file can be written is_writable () // determine whether the file can be written file_exists () // whether the file exists

Code example: $ filename = 'test.txt '; $ somecontent; // first, we need to confirm that the file exists and can write if (is_writable ($ filename) {// in this example, we will use the Add mode to open $ filename. Therefore, the file pointer will start with the file, that is, the place where $ somecontent will be written when we use fwrite. If (! $ Handle = fopen ($ filename, 'A') {echo "cannot open the file $ filename"; exit;} // write $ somecontent to the file we open. If (fwrite ($ handle, $ somecontent) === FALSE) {echo "cannot be written to the file $ filename"; exit ;} echo "successfully writes $ somecontent to the file $ filename"; fclose ($ handle);} else {echo "File $ filename cannot be written ";}

9. Read the file to an array:

$ Array = file ("text.txt"), $ array [0] is the first line of text, and so on. If you want to flip the entire array, for example:
$ Arr = array_reverse ($ array );
The last line of text is $ arr [0].

10. Access the Directory
Forward slash (/) is recommended for directory access. It is compatible with windows and unix systems. Main functions include:

Basename () // returns the file name dirname () that does not contain the path information. // returns the realpath () part of the directory of the file name. // accepts the relative path and returns the absolute path pathinfo () of the file () // extract the directory name, basic file name, and extension opendir () from the specified path // open the Directory and return the resource handle readdir () // read the directory item rewinddir () // returns the read pointer to the beginning of closedir () // closes the read handle chdir () // Changes the current working directory mkdir () during the execution of the current script // creates the directory rmdir () delete directory

Appendix: php file functions

Basename-part of the file name in the return path chgrp-change the group to which the file belongs chmod-change the file mode chown-change the file owner clearstatcache-Clear the File status cache copy-copy the file delete-see unlink () or unset () dirname-return directory part disk_free_space-return available space disk_total_space in the directory-return the total disk size of a directory diskfreespace-disk_free_space () alias fclose-close an opened file pointer feof-test whether the file pointer has reached the end of the file fflush-output the buffer content to the file fgetc-read the character fgetcsv from the file pointer-from read a row from the file pointer and parse the CSV field fgets-read a row from the file pointer fgetss-read a row from the file pointer and filter out the HTML Tag file_exists-check whether the file or directory contains file_get_contents- read the entire file into a string file_put_contents-write a string to the file-read the entire file into an array fileatime-get the last access time of the file filectime-This PHP file function gets the inode modification of the file time filegroup-Get File Group fileinode-Get File inode filemtime-Get file modification time fileowner-Get file owner fileperms-Get File Permission filesize-Get File Size filetype-Get File flock-lightweight consulting File Locking fnmatch-pattern matching file name fopen-open file or URL fpassthru-all the remaining data at the output file pointer fputcsv-format the row as CSV and write it into the file pointer fputs-fwrite () alias fread-Read File (which can be safely used for binary files) fscanf-format the input fseek from the file-locate fstat in the file pointer-get the file information through the opened file pointer ftell-return the file pointer read/write position ftruncate-truncate the file to the given length fwrite-Write File (can be safely used for binary files) glob-find the file path is_dir that matches the pattern-This PHP file function determines whether the given file name is a directory is_executable-determines whether the given file name can be executed is_file-determines whether the given file name is a normal file is_link -Determine whether the given file name is a symbolic connection to is_readable-determine whether the given file name is readable is_uploaded_file-determine whether the file is is_writable uploaded through http post-determine whether the given file name can be written to is_writeable-is_writable () link-create a hard connection linkinfo-get a connection information lstat-give a file or symbolic connection information mkdir-create a directory move_uploaded_file-move the uploaded file to the new location parse_ini_file- parse a configuration file pathinfo-return the file path information pclose-close the process file pointer popen-open the process file pointer readfile-output a file readlink-return the destination realpath to which the symbolic connection points-return the Normalization rename-rename the absolute path name of a file or directory rewind-returns the position of the file pointer rmdir-delete the directory set_file_buffer-stream_set_write_buffer () alias stat-File Information symlink-Establish a symbolic connection tempnam-create a file tmpfile with a unique file name-create a temporary file touch-set the file access and modification time umask-change current umask unlink-delete file

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.