File manipulation has always been a headache for web programmers, and file operations are necessary in systems such as CMS. Now, the php file operation function content is very powerful, the file is also a very important part of learning PHP, I hope you do not ignore. This article will be a brief introduction of PHP several basic file operations, and finally attached to the PHP file function summary for your reference and learning.
1. Open File: fopen ("filename", "mode");
Mode parameters:
R: Read-only, pointing the file pointer to the file header. r+: Read/write, point the file pointer to the file header. W: Write only, open and empty the contents of the file. If the file does not exist, the file is created. w+: Read/write, open and empty the contents of the file. If the file does not exist, the file is created. 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 to preserve the contents of the file by writing to the end of the file.
2. Close file: fclose ();
After using the file, explicitly tell PHP that you have finished using the file, for example:
$file =fopen ("Test.txt", "R"); Close an open file pointer//some code be executed fclose= ($file);
3. Detect if the end of the file has been reached: feof ();
For example: if (feof ($file)) {echo "End of File";}
4. Read file by line: Fgets ();
For example:
$file =fopen ("Test.txt", "R"); while (!feof ($file)) { echo fgets ($file). " < br/> "; }
5. Read files by character: Fgetc ()
6. Read any binary data: fread ()
7. Determine the status of the file read
Each file handle has a file pointer, which, depending on the mode parameter of the fopen function, is initially located at the beginning of the file or at the end of the file. Feof () can tell if the file has reached the end, and the FileSize () function returns the size of the file.
8. Write file and permission judgment
Fwrite () function performs file write is_readable ()//Determine if the file is readable is_writeable ()//Determine if the file is writable is_writable ()//Determine if the file can be written file_exists ()//whether the file exists
code example: $filename = ' test.txt '; $somecontent;//First we want to make sure that the file exists and is writable if (is_writable ($filename)) { //In this example, we will use Add mode to open $filename, so The file pointer will be at the beginning of the file, which is where $somecontent will write when we use Fwrite (). if (! $handle = fopen ($filename, ' a ')) { echo "Cannot open file $filename"; Exit; } Write the $somecontent to the file we opened. if (fwrite ($handle, $somecontent) = = = FALSE) { echo "cannot write to file $filename"; Exit; } echo "successfully wrote $somecontent to file $filename"; Fclose ($handle); } else{ echo "file $filename not writable";}
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. Accessing the Directory
Directory access is recommended to use forward slash "/" compatible with Windows and UNIX systems. The main functions are:
BaseName ()//returns the file name that does not include the path information dirname ()//Returns the directory portion of the file name Realpath ()//accepts relative path, returns the absolute path of the file PathInfo ()//extracts the directory name of the given path, Basic file name and extension Opendir ()//Open Directory, return resource handle readdir ()//Read directory entry Rewinddir ()//Read the pointer back to the beginning Closedir ()//close read handle chdir ()// Changes the current working directory during the current script execution mkdir ()//Create directory rmdir () Delete directory
Attached: PHP file functions Daquan
basename-return the file name part of the path chgrp-change the group chmod-file mode chown-Change the file's owner clearstatcache-clear file state cache copy-copy file delete-see Unlink () or unset () dirname-returns the directory portion of the path disk_free_space-returns the free space in the directory disk_total_space-the total size of the disk that returns a directory Diskfreespace-disk_ Free_space () alias fclose-close an open file pointer feof-test file pointer to the end of the file fflush-output buffered content to a file fgetc-reads a character from the file pointer fgetcsv-reads a line from the file pointer And parses the CSV field fgets-reads a line from the file pointer fgetss-reads a row from the file pointer and filters out the HTML tag file_exists-checks if the file or directory exists file_get_contents-reads the entire file into a string f Ile_put_contents-writes a string to the file file-reads the entire file into an array fileatime-the last access time of the file filectime-this php file function gets the Inode modification time of the file filegroup -Get the file of the group fileinode-get the file's inode filemtime-get the file modification time fileowner-get the file owner fileperms-get file permissions filesize-get file size filetype -Get file Type flock-Lightweight consultation file lock fnmatch-with pattern matching file name fopen-Open file or URL fpassthru-output file pointer to all remaining data fputcsv-format the row as CSV and write the file pointer f Puts-fwrite () alias fread-read file (safe for binary) fscanf-format input from file fseek-position in file pointer fstat-get file information via open file pointer ftell-return file pointer read/write The location of ftruncate-truncates the file to the givenThe length of fwrite-writes to the file (which is safe for binary files) glob-look for a file path that matches the pattern is_dir-the php file function to determine whether a given file name is a directory is_executable-determine whether a given file name can be executed is_file-sentenced If the given file name is a normal file is_link-determine if the given file name is a symbolic connection is_readable-determine if the given file name is readable is_uploaded_file-determine if the file is uploaded via HTTP POST is_writab Le-determines whether the given file name can be written is_writeable-is_writable () alias link-establish a hard connection linkinfo-get a connection information lstat-give a file or symbolic connection information mkdir-new directory move_uploaded_file-move the uploaded file to a new location parse_ini_file-parse a profile pathinfo-return the file path information pclose-Close the process file pointer popen-Open the process file pointer Readfi le-output A file readlink-return a symbolic connection to the destination realpath-return the normalized absolute pathname rename-rename a file or directory rewind-reverse the location of the file pointer rmdir-Delete directory Set_file_buf Fer-stream_set_write_buffer () alias stat-give the file information symlink-establish a symbolic connection tempnam-create a file with a unique file name tmpfile-set up a temporary file touch- umask-Change the current umask unlink-delete files by changing the access and modification time of the
PHP Simple Introduction to file operations