PHP file read and read file output examples

Source: Internet
Author: User
Tags fread php file readfile strlen

One, read the file

First explain what is reading the file itself, what is called reading the file input. For example test.php inside the content <?php echo "test";?>

1, read the file itself is to read all the contents of the file, read it can get <?php echo "test";?>
2, read the file output is to read the contents of the file, read after the test

Second, fopen method

1, read the file itself

<?php
$filename = "test.php";
$handle = fopen ($filename, "R");
$contents = Fread ($handle, FileSize ($filename));
Fclose ($handle);
echo strlen ($contents);
?>

2, read the file output

<?php
$filename = "http://localhost/test/test.php";
$handle = fopen ($filename, "R");
$contents = "";
while (!feof ($handle)) {
$contents. = Fread ($handle, 8192);
}
Fclose ($handle);
echo strlen ($contents);
?>

The above two read files are the same one, but why is it different, http://localhost/test/test.php, where the test.php file is explained, fopen just get the script entered the content, see the PHP official website explain it
fopen () binds the name resource specified by filename to a stream. If filename is "scheme://..." Format, the Search protocol processor (also known as the Encapsulation Protocol) is treated as a url,php to handle this pattern. If the protocol has not yet registered the encapsulation protocol, PHP will issue a message to help check for potential problems in the script and continue with filename as a normal filename.

Three, the file method

1, read the file itself

<?php
$filename = "test.php";
$content = file ($filename); Get array
Print_r ($content);
?>
2, read the file display output
<?php
$filename = "http://localhost/test/test.php";
$content = file ($filename);
Print_r ($content);
?>

Four, file_get_contents method

1, read the file itself

<?php
$filename = "test.php";
$content = file_get_contents ($filename); Get string
echo strlen ($content);
?>
2, read the file display output
<?php
$filename = "http://localhost/test/test.php";
$content = file_get_contents ($filename);
echo strlen ($content);
?>

Five, ReadFile method

1, read the file itself

<?php
$filename = "test.php";
$num = ReadFile ($filename); Returns the number of bytes
Echo $num;
?>
2, read the file display output

<?php
$filename = "http://localhost/test/test.php";
$num = ReadFile ($filename); Returns the number of bytes
Echo $num;
?>

Vi. the Ob_get_contents method

1, read the file display output

<?php
Ob_start ();
Require_once (' bbb.php ');
$content = Ob_get_contents ();
Ob_end_clean ();
echo strlen ($content);
?>

Summarize

PHP, a lot of ways to read the file, read the URL method is also a lot of individuals summed up a bit, if there is no please correct me, if there is insufficient please add.

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.