PHP/File Directory/File Read

Материал из Web эксперт
Перейти к: навигация, поиск

File read by char

   <source lang="html4strict">

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

} ?> </body> </html>


      </source>
   
  


Opening and Reading a File Line by Line

   <source lang="html4strict">

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

} ?> </body> </html>


      </source>
   
  


Reading a Specific Character

   <source lang="html4strict">
 <?php
 $myfile = "./test.txt";
 $openfile = fopen ($myfile, "r") or die ("Couldn *** open the file");
 fseek ($openfile, 8);
 $chunk = fgetc ($openfile);
 echo $chunk;
 ?>
          
      </source>
   
  


Reading from a File

   <source lang="html4strict">
<?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;
 ?>
          
      </source>
   
  


Reading Specific Data from a File

   <source lang="html4strict">

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

?>

      </source>
   
  


Using fgets() and feof() Functions

   <source lang="html4strict">

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

?>

      </source>