First is a file to see whether can read (permission problem), or exist no, we can use the Is_readable function to get information.:
Php/func_filesystem_is_readable.htm ">is_readable function usage
| The code is as follows |
Copy Code |
<?php $file = "Test.txt"; if (is_readable ($file)) { Echo ("$file is readable"); } Else { Echo ("$file is not readable"); } ?> |
Output:
Test.txt is readable
The file_get_contents function is used to read files, which can read large amounts of data, or read remote server files, but must be php.ini start Allow_url_fopen = On otherwise this function is not available.
| The code is as follows |
Copy Code |
<?php $file = "filelist.php"; if (file_exists ($file) = = False) { Die (' File not present '); } $data = file_get_contents ($file); echo htmlentities ($data); ?> |
Read the remote file, this is the topic outside this tutorial.
| The code is as follows |
Copy Code |
function Vita_get_url_content ($url) { if (function_exists (' file_get_contents ')) { $file _contents = file_get_contents ($url); } else { $ch = Curl_init (); $timeout = 5; curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, $timeout); $file _contents = curl_exec ($ch); Curl_close ($ch); } return $file _contents; } |
Using the Fread function
To read the file, this function can read the amount of data specified
Fread read a file instance
| The code is as follows |
Copy Code |
$filename = "/www.111cn.net/local/something.txt"; $handle = fopen ($filename, "R"); $contents = Fread ($handle, FileSize ($filename)); Fclose ($handle); |
Read the remote server content php5 the above version
| The code is as follows |
Copy Code |
$handle = fopen ("http://www.111cn.net/", "RB"); $contents = Stream_get_contents ($handle); Fclose ($handle); |
There is also a way to read binary files:
| The code is as follows |
Copy Code |
$data = Implode ("", File ($file)); |
Write operations for fwrite files
Fwrite () writes the contents of a string to the file pointer files. If length is specified, when the length byte is written or a string is written, the write stops, depending on which case is first encountered.
Fwrite () returns the number of characters written and false when an error occurs.
File Write function:
| code is as follows |
copy code |
<?php / /File Write function function php_write ($file _name, $data, $method = "W") { $filenum = @fopen ($file _name, $method); Flock ($ FILENUM,LOCK_EX); $file _data=fwrite ($filenum, $data); Fclose ($filenum); return $file _data; } |