I have a file named "connection.php". I want to read the contents of this file to a string. i use fopen, and read functions for reading. But when i am reding i just got only last 2-3 lines on that file. That means no php scripts canot read like echo , fuctions etc... How can i read the whole contents on that file??
<?php
$str = file_get_contents('connection.php');
var_dump($str);
?>
note that if 'connection.php' contains '<?php'
at the beginning, and you try to output it to a browser, you likely won't see anything unless you perform a "View Source".
Hi,
Quoting the manual page of fread
:
fread()
reads up to length bytes from the file pointer referenced by handle . Reading stops as soon as one of the following conditions is met:
- length bytes have been read
- EOF (end of file) is reached
- a packet becomes available (for network streams)
- 8192 bytes have been read (after opening userspace stream)
If you want to read a whole file, you'll need to use some kind of loop, to read data until you reach the end of the file.
Or, as an alternate (probably easier), you can use file_get_contents
, which will get you the whole content of the file with only one function call.
Which means no need for fopen
+ multiple fread
+ fclose
;-)
Perhaps your browser is hiding the content because it starts with '<?php
'. You can try View Source in your web browser, or echo the contents in the following way:
<?php
$contents = file_get_contents('connection.php');
echo "<pre>";
echo htmlentities($contents);
echo "</pre>";