PHP/File Directory/Text File Read

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

Display XML file

   <source lang="html4strict">

<?php $xml_string = file_get_contents("contact.xml","rb"); echo $xml_string; ?>


      </source>
   
  


Read file content with only one command

   <source lang="html4strict">

<html> <head> <title>Tip of the day</title> </head> <body>

Tip of the day

<? readfile("tips.txt"); ?>

</body> </html>

      </source>
   
  


Read in and perform operations on a file line by line

   <source lang="html4strict">

<? $fd = fopen("test.txt", "r"); while(!feof($fd)) {

 $line = fgets($fd, 4096);
 if(strcmp($line,$targetline) == 0) {
    echo "A match was found!";
 }

} fclose($fd); ?>

      </source>
   
  


Read text file into an array in one function

   <source lang="html4strict">

<? $line_array = file("test.txt"); echo $line_array[0]; ?>

      </source>
   
  


Read text file into array and output

   <source lang="html4strict">

<?php

  $userfile= file_get_contents("./text.txt");
  // Place each line of $userfile into array
  $users = explode("\n",$userfile);
  foreach ($users as $user) {
     list($name, $email) = explode(" ", $user);
     echo "<a href=\"mailto:$email\">$name/a> 
"; }

?>


      </source>
   
  


Read text file with format

   <source lang="html4strict">

<?php

  $fh = fopen("intData.txt", "r");
  /* Parse each SSN in accordance with integer-integer-integer format. */
  while ($user = fscanf($fh, "%d-%d-%d")) {
  list ($part1,$part2,$part3) = $user;
  echo "Social Security: $part1-$part2-$part3 
"; } fclose($fh);

?>


      </source>
   
  


Try to open a text file

   <source lang="html4strict">
 <?php
 $openfile ="./test.txt";
 $result = fopen ($openfile,"r") or die ("Couldn"t open the file");
 echo "File opened successfully";
 ?>
          
      </source>
   
  


Viewing the Source of a Document

   <source lang="html4strict">

<html> <head> <title>Viewing the source of a document</title> </head> <body> <form action="<?php print $PHP_SELF ?>" method="get"> Enter file name (test.txt): <input type="text" name="file" value="<?php print $file; ?>">


<?php if ( isset( $file ) )

   show_source( $file ) or print "couldn"t open \"$file\"";

?> </body> </html>

      </source>