File operations are required for every language, not just PHP, here we only use PHP to explain
PHP file Advanced operations and file upload examples I put in the last part of the article. --I will also write a php similar to the operation of the network disk example
Note: Read this article, must have a "PHP Chinese manual" or other can query PHP function manual Because the article has a large number of examples, but in order to save space and time, so please consult the PHP manual to understand the function of the specific role
1. PHP open File and close file
Functions used fopen (), fclose () Note: Open files must be closed! Please refer to the PHP manual for specific function parameters.
Instance:
View Code
The fopen () function also supports URL formats in PHP5:
View Code
2. php file Read and write
Read operation-
Functions used ReadFile (), file (), file_get_contents (), fgets (), FGETSS (), fgetc ()
The file location in the example is changed according to your own file, do not tangle with the file location and filename or whatever. Focus on learning
<title>File test</title>
Use the ReadFile () function to read the contents of the file: |
|
Use the file () function to read the contents of files: |
"; }?> |
Use the file_get_contents () function to read the contents of the file: |
|
Use the Fgets () function: |
|
Use the FGETSS () function: |
|
Use the Fgetc () function: |
|
View Code
Write operation-
Functions used Fwrite (), file_put_contents ()
"; Echo fread ($fp, FileSize ($filename)); Output the rest of the file contents */ $filepath = "05.txt"; $str = "The feeling can be recalled just now disconsolate
"; echo "writes the file with the fwrite () function:"; $fopen = fopen ($filepath, "WB") or Die ("file does not exist"); Fwrite ($fopen, $str); Fclose ($fopen); ReadFile ($filepath); echo "Write the file with the File_put_contents () function: "; File_put_contents ($filepath, $str); ReadFile ($filepath);? >
View Code
3, the PHP file itself operation
This example simply enumerates functions and does not run directly. Please change it according to your needs before running
View Code
===================================================================
One, PHP directory operation
Similar to file operations, open and close directories with Opendir (), and Closedir () functions.
View Code
Second, the PHP directory of the Tour
Core function Scandir ()
"; } } else { echo ' directory path Error! "; }? >
View Code
Third, the PHP directory of other operations, get the disk directory size, new delete directory and so on
This example simply enumerates functions and does not run directly. Please change it according to your needs before running
"; ChDir (".. /"); Change to return the previous level of echo GETCWD ();//Retrieve the current directory */* Use the Disk_free_space () function to return the available space in the directory * /Echo disk_free_space ("d:/")/ Pow (1024,3); This translates to G, the default is in bytes */ * with the Disk_total_space () function to return the available space in the directory */ echo disk_total_space ("d:/")/pow (1024,3); This is converted to G, the default is in bytes to display the */* about PHP5 before the old tour directory method I do not list, here just give the function name: Readdir (), Rewinddir () */?>
View Code
====================================================================
Advanced Application of documents
File pointers:
"; Total number of bytes output $fopen = fopen ($filename, "RB"); To open the file, go to echo in the read-only binary file format "Initial pointer position is:". Ftell ($fopen). "
"; Output pointer position fseek ($fopen, 3); Move the pointer echo "Use the fseek () function after the pointer position:". Ftell ($fopen). "
"; The position of the pointer after the output is moved echo "outputs the contents after the current pointer:". fgets ($fopen). "
"; Outputs the contents from the current pointer to the end of the line if (feof ($fopen)) //Determines whether the pointer points to the end of the file { echo "the current pointer points to the end of the file:". Ftell ($fopen). "
"; If you point to the end of the file, the output pointer position } //Use the rewind () function to return the beginning of the document stream- once the stream is read to the end, it will not be available again, //So here's a pointer to a file again. Rewind ($fopen); Look at the position of the pointer after using the rewind () function echo the position of the pointer after using the REWIND () function: ". Ftell ($fopen)."
"; echo "Outputs the first 6 bytes of content:". Fgets ($fopen, 6); Outputs the first 6 bytes of content fclose ($fopen); Close File } else { echo "file does not exist!" }? >
View Code
File Lock:
View Code
====================================================================
Uploading of files
Get information about uploading a file:
Untitled Document
$value) //Use the Foreach Loop output to upload the name and value of the file information { echo $name. ' = '. $value. '
"; } } ? >
View Code
Real file uploads (from temporary to specified location):
Untitled Document
0) //Determine file size { //upload file move_uploaded_file ($fileinfo [' Tmp_name '], "upfile/". $fileinfo [' name ']); echo "File upload succeeded!"; } else { echo "file too large or unknown"; } } ? >
View Code
Multiple file uploads:
Untitled Document
"; } } } ? >
View Code