PHP/File Directory/File Read
Содержание
File read by char
<html>
<head>
<title>Moving around a file with fseek()</title>
</head>
<body>
<?php
$filename = "test.txt";
$fp = fopen( $filename, "r" ) or die("Couldn"t open $filename");
while ( ! feof( $fp ) ){
$char = fgetc( $fp );
print "$char<BR>";
}
?>
</body>
</html>
Opening and Reading a File Line by Line
<html>
<head>
<title>Opening and reading a file line by line</title>
</head>
<body>
<?php
$filename = "test.txt";
$fp = fopen( $filename, "r" ) or die("Couldn"t open $filename");
while ( ! feof( $fp ) ) {
$line = fgets( $fp, 1024 );
print "$line<br>";
}
?>
</body>
</html>
Reading a Specific Character
<?php
$myfile = "./test.txt";
$openfile = fopen ($myfile, "r") or die ("Couldn *** open the file");
fseek ($openfile, 8);
$chunk = fgetc ($openfile);
echo $chunk;
?>
Reading from a File
<?php
$myfile = "./test.txt";
$openfile = fopen ($myfile, "r") or die ("Couldn"t open the file");
$file_size=filesize($myfile);
$file_content = fread ($openfile, $file_size);
fclose ($openfile);
echo $file_content;
?>
Reading Specific Data from a File
<?php
$myfile = "./test.txt";
$openfile = fopen ($myfile, "r") or die ("Couldn"t open the file");
$filesize =filesize($myfile);
fseek ($openfile, 8);
$sp_data = fread ($openfile, ($filesize - 8)) ;
print $sp_data;
?>
Using fgets() and feof() Functions
<?php
$myfile = "./test.txt";
$openfile = fopen ($myfile, "r") or die ("Couldn"t open the file");
while (!feof ($openfile)) {
$lines= fgets($openfile, 1024);
echo "$lines", "\n";
}
?>