PHP/File Directory/fwrite

Материал из Web эксперт
Версия от 07:04, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Appending to a file

 
<?php  
  # for Linux use...
  # $filename = "/home/mike/Desktop/newfile.txt";
  # for Windows use...
  $filename = "C:\\newfile.txt";
  $file = fopen( $filename, "a" );
  $string = "\t\t\tOctober 13,2010";
  fwrite( $file, $string );
  fclose( $file );
?>
<html>
 <head>
  <title>Appending to a file</title>
 </head>
 <body>
 <?php
  $file_length = filesize( $filename );
  echo( "$filename is now $file_length bytes" );
 ?>
 </body>
</html>



A simple access counter

 
<?
    $access = "data.txt";               
    $visits = @file($access);           
    $current_visitors = $visits[0];     
    ++$current_visitors;                
    $fh = fopen($access, "w");          
    @fwrite($fp, $current_visitors);    
    fclose($fh);                        
?>



fwrite() function writes string to the file.

 
Its syntax is: int fwrite (int filepointer, string string [, int length])
Writing stops either after length characters have been written or after the end of string has been reached. 
<?
$data = "data";
$filename = "data.txt";
if ( is_writeable($filename) ) :
     $fh = fopen($filename, "a+");
     $success = fwrite($fh, $data);
     fclose($fh);
else :
     print "Could not open $filename for writing";
endif;
?>



fwrite.php

 
<?php
   $subscriberInfo = "data";
   $fh = fopen("/data.txt", "at");
   fwrite($fh, $subscriberInfo);
   fclose($fh);
?>



Storing user information in a text file

 
<html>
<body>
<?
$form ="<form action=\"index.php\" method=\"post\">
<input type=\"hidden\" name=\"seenform\" value=\"y\">
<b>Give us your personal info!</b><br>
Your Name:<br>
<input type=\"text\" name=\"name\" value=\"\"><br>
Your Email:<br>
<input type=\"text\" name=\"email\" value=\"\"><br>
Your Preferred Language:<br>
<select name=\"language\">
<option value=\"\">Choose a language:
<option value=\"English\">English
<option value=\"Spanish\">Spanish
</select><br>
Your Occupation:<br>
<select name=\"job\">
<option value=\"\">What do you do?:
<option value=\"student\">Student
<option value=\"programmer\">Programmer
</select><br>
<input type=\"submit\" value=\"submit!\">
</form>";
if ($seenform != "y") :
     print "$form";
else :
     $fd = fopen("user_information.txt", "a");
     $name = str_replace("|", "", $name);
     $email = str_replace("|", "", $email);
     $user_row = $name."|".$email."|".$language."|".$job."\n";
     fwrite($fd, $user_row) or die("Could not write to file!");
     fclose($fd);
     print "Thank you!";
endif;
?>
</body>
</html>



Using file-related error information

 
<?php
$fh = fopen("/tmp/cookie-data","w")      or die("can"t open: $php_errormsg");
if (-1 == fwrite($fh,$_COOKIE["flavor"])) { die("can"t write: $php_errormsg") };
fclose($fh)                              or die("can"t close: $php_errormsg");
?>



Writing data to a file

 
<?php
$fh = fopen("/tmp/cookie-data","w")      or die("can"t open file");
if (-1 == fwrite($fh,$_COOKIE["flavor"])) { die("can"t write data"); }
fclose($fh)                              or die("can"t close file");
?>