PHP development Notes Series (4)-file operations

Source: Internet
Author: User
PHP development Notes Series (4)-file operations ??? For general web applications, data is stored in database tables, but file operations are also required, such as reading and writing files. A typical application scenario is exam registration, when the system starts, it automatically reads the "Registration Notice" content to the memory and displays it on the page when necessary. This article "PHP development Notes Series (IV)-file operations" PHP development Notes Series (IV)-file operations

??? For general web applications, data is stored in database tables, but file operations are also required, such as reading and writing files. A typical application scenario is exam registration, when the system starts, it automatically reads the "Registration Notice" content to the memory and displays it on the page when necessary. This article "PHP development Notes Series (4)-file operations" will study file operations in PHP.

?

1. read files to an array (file)

??? The file () function can read files into a string array by row. Subsequent programs can traverse this string array for processing.

file:file.phpurl:http://localhost:88/file/file.php
 
?

2. read the file to the string (file_get_contents)
??? The file_get_contents () function can read all the content in the file to a string variable. later programs can display the content in the string on the page or write it to a new file.

file:file_get_contents.phpurl:http://localhost:88/file/readfile.php
 
?


3. read the file to the screen (readfile)
??? The readfile () function directly reads the file content and returns it to the screen. This function is also used during upload or download.

file:readfile.phpurl:http://localhost:88/file/readfile.php
 

?


??? The above method can only read the content in the file in whole or in accordance with the newline, read the content in the array or string or screen, and then if you need to read the file content or write the file content more flexibly, the file handle is used, which is similar to the file read/write function in C language.

4. Read the entire file at one time (fread, filesize)
??? For a file with a relatively small size, you can read the entire file content to the memory at a time. Step 1: open a new file with fopen and specify the usage mode; 2) read the file content with fread according to the specified length; 3) close the handle with fclose.

file:fread.phpurl:http://localhost:88/file/fread.php
 
?


5. read the content of the entire file in multiple times (fgets, feof)

??? The fgets function can obtain the content of a file in parts. feof can judge whether the file has been read to the end.

file:fgets.phpurl:http://localhost:88/file/fgets.php
 
?


6. one Read (file_get_contents) and one write (file_put_contents)
??? Sometimes, you can use the file_get_contents () and file_put_contents () methods to write small files without creating a file handle.

File: readOnce_writeOnce.phpurl: http: // localhost: 88/file/readOnce_writeOnce.php
 

?


7. read by branch, write by branch (fwrite)
??? For reading and writing large files, if the content is loaded once, it is likely to cause memory overflow or the web server is down. Therefore, you need to read and write data row by row. You can use the number of lines in file () to read the source file to a string array, and then write the source file to the target file row by row through fwrite. of course, you need to use fopen to open the handle and use fclose to close the handle after writing.

file:multiRead_multiWrite.phpurl:http://localhost:88/file/multiRead_multiWrite.php
 
?

8. Other file processing functions

File: other. phpurl: http: // localhost: 88/file/other. php
 '; // Is_file () returns 1 or 0 echo $ fileName. 'is'. is_file ($ fileName )? 'File': 'directory '.'! '.'
'; // Is_readable () returns 1 or 0 echo $ fileName. 'readable:'. is_readable ($ fileName ).'
'; // Is_writable () returns 1 or 0 echo $ fileName. 'writable:'. is_writable ($ fileName ).'
'; Echo $ fileName. 'created at:'. filemtime ($ fileName ).'
'; Echo $ fileName. 'created at:'. fileatime ($ fileName ).'
'; Echo $ fileName. 'Size:'. filesize ($ fileName ).'
';?>

?

?

9. directory reading (opendire, readdir, closedir)

??? The first method is to use the opendir, readdir, and closedir functions to open, read, and close directories, similar to fopen, fread, and fclose of files. the code is as follows:

file:dir.phpurl:http://localhost:88/file/dir.php
 ';        }elseif (is_file($entry))        {            echo '[FILE] '.$entry. '
'; } } closedir($dp);?>

?

??? The second method uses the object-oriented dir class provided by php to construct a dir instance by passing in a directory path, and then reads the directory through the read method of the instance. the code is as follows:

file:dir-class.phpurl:http://localhost:88/file/dir-class.php
 read()){        if(is_dir($entry))        {            echo '[DIR] '.$entry. '
'; }elseif (is_file($entry)) { echo '[FILE] '.$entry. '
'; } } closedir($dp);?>
?

10. copy)

file:copy.phpurl:http://localhost:88/file/copy.php
 

?

?

11. file movement and rename)

 

?

???Address: http://ryan-d.iteye.com/blog/1543374

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.