PHP file read and write operation

Source: Internet
Author: User

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;
}

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.