File and directory actions
PHP is convenient for processing files and directories on the local server, but sometimes permissions and path-related problems
1. Open File
Resource fopen (string filename , string mode [, bool Use_include_path [, Resource Zcontext]])
$handle = fopen (filename,mode)//Open file, returned to represent this article The
file name can use a relative path or an absolute path or network protocol mode, and the open mode has rr+ww+aa+xx+b
If you do not specify a ' B ' mark when manipulating a binary file, the &NBSP There may be some strange problems, including bad picture files and strange questions about RN characters. &NBSP
For portability considerations, it is strongly recommended that you always use the ' B ' flag when opening a file with fopen (). &NBSP
The following are several ways to open a file
$fp = @fopen (' Log.txt ', ' RB ');
$fp = @fopen ('. /log.txt ', "RB");
$fp = @fopen ("http://www.runer.com.cn/default.htm", "RB");//You can also use protocols such as FTP and Ghoper. You must enable the Allow_url_fopen option in the php.ini file;
////////////////////////////Code Section//////////////////////////////// &NBSP
$filename 1 = "Userinfo.txt"; the file exists in the directory or include_path
$filename 2 = "test.txt"; /directory or include_path does not exist this file
$resource 1 = fopen ($filename 1, "RB")
@ $resource 2 = FOPEn ($filename 2, "RB");//Because this file does not exist in the directory and is not used or include_path to find the path where the containing file is located, this action will complain, and using the error suppressor @ can force the browser not to output the error message
If ($resource 1)
echo "Open file {$filename 1} succeeded";
if (! @fopen ($filename 2, "R"))
Echo "Open File {$filename 2} is unsuccessful";
////////////////////////////////////////////////////////////////////////// &NBSP
---------------------output----------------------------------------
Open File Userinfo.txt successful
---------------------------------------------------------------------
2. After using the file, you should explicitly tell PHP that you have finished using the file, and let the operating system ensure that all contents of the file are flushed correctly from the buffer to the hard drive
Use fclose () to close the file,
BOOL Fclose (resource handle)//closes an open file pointer
3. Read the file, the mode parameter of the fopen function allows reading, PHP provides several functions to read data from the file
string fgets (int handle [, int length]) reads a row from the file pointer, Trying fgets on a binary file produces unpredictable results
If you do not specify a length, the default reads 1K data, stops the when it encounters a newline character (including in the return value), EOF, or has read the length-1 byte;
String FGETSS (resource handle [, int length [, string allowable_tags]]) reads a row from the file pointer and filters out HTML tag
FGETC () reads a single character
Fread () reads arbitrary binary data
////////////////////////////Code Section///////////////////// &NBSP
$handle = fopen ("Test.jpg", "RB"),
$c;
while!feof ($ha Ndle)) {
$contents. = @fread ($handle, 8192);//loop Read and merge it into a large file
}
Fclose ($ handle);
//////////////////////////////////////////////////////////////////////////
---------------------output----------------------------------------
------------------------------- --------------------------------------
4. Determine the state of the file read &nbSp
Each file handle has a file pointer, or a cursor that indicates where the next operation will occur in the file, depending on the mode parameter of the fopen function the
file pointer is initially at the beginning of the file (0), or at the end of the file The
feof () can determine whether the file is at the end (the function returns true after the end) the
FileSize () function returns the size of the file 5. write file
fwrite () function performs file write &NBSP
////////////////////////////Code part////////////////////////////////////////
$filename = ' Test.txt ';
$somec;
//First we want to make sure that the file exists and is writable. &NBSP
if (is_writable ($filename)) {
//In this example, we will use Add mode to open the $filename,
//Therefore, The file pointer will be at the beginning of the file,
, or that is where we $somecontent will write when we use Fwrite (). &NBSP
if (! $handle = fopen ($filename, ' a ')) {
echo "Cannot open file $filename";
exit;
}
//writes $somecontent to the file we open. &NBSP
if (fwrite ($handle, $somecontent) = = FALSE) {
echo "cannot be written to the file $filename";
Exi T;&NBSP
}
Echo successfully writes the $somecontent to the file $filename ";
Fclose ($handle);
br>} else {
}
echo "file $filename not writable";
///////////////////////////////////// &NBSP
--------------------- The output----------------------------------------
Successfully writes the added text to the file test.txt
------------ ---------------------------------------------------------
for binary data, you must specify a third parameter that contains the number of data bytes written to disk
$result = @fwrite ($fp, $binary _data,mb_strlen ($binary _data, ' 8bit ');
|
&N BSP; |
6. File permissions and other information Is_readable ()/To determine whether the file is readable Is_ Writeable ()//To determine whether the file can be written is_writable ()//To determine whether the file is writable Fileperms ()//Determine file Permissions (Unix-style file permissions test function) &NBSP File_exists ()/whether there is a file Fileowner ()//To determine the user to which the file belongs Filegroup ()/To determine the group to which the file belongs &NBSP 7. Delete and rename the file Unlink ()//delete file Rename ()/rename file 8. Access Directory Directory Access recommends the use of forward slash "/", compatible with Windows and UNIX systems basename ()//returns a filename that does not include path information DirName ()// Returns the directory portion of the file name Realpath ()/Accept the relative path, returning the absolute path the file, PathInfo ()//fetching the directory name of the given path, the base file name and the extension Opendir ()//Open Directory, return resource handle Readdir ()/Read directory entry Rewinddir ()//Returns the read pointer to the beginning &NBSP Closedir ()//closes the read handle ChDir ()//changing the current working directory during the current script execution mkdir ()//Create directory rmdir () delete directory ////////////////////////////Code Section////////////////// &NBSP ////////////////////////////////////////////////////////////////////////// &NBSP ---------------------output---------------------------------------- filename: WEB&NBSP: filetype:dir Filename:study:filetype:dir Reproduced in www.abm163.com |
|