PHP/File Directory/fopen

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

Acceptable fopen() Modes

   <source lang="html4strict">

r Open the file for reading.

r+ Open the file for reading and writing.

w Open the file for writing, overwriting existing files, and creating the file if it does not exist.

w+ Open the file for reading and writing, overwriting existing files, and creating the file if it does not exist.

a Open the file for writing, creating the file if it does not exist, and appending to the file if it does.

a+ Open the file for reading and writing, creating the file if it does not exist, and appending to the file if it does.

b Open the file in binary reading/writing mode (applicable only on Windows systems; however, recommended in all scripts).

 </source>
   
  


Calling fopen() with a Context Resource

   <source lang="html4strict">

<?php $url = "http://www.wbex.ru/index.htm"; $options = array ("http" => array ("user_agent" => "php24-test-script", "header" => "referer: http://www.example.ru/index.html\r\n" ) ); $context = stream_context_create ( $options ); $res = fopen ( $url, "r", 0, $context ) or die ( "could not open page" ); while ( ! feof ( $res ) ) {

 print fgets ( $res, 1024 );

} ?>

 </source>
   
  


File open with exception checking

   <source lang="html4strict">

<?php try {

   $fh = fopen("data.txt", "r");
   if (! $fh) {
       throw new Exception("Could not open the file!");
   }

} catch (Exception $e) {

   echo "Error (File: ".$e->getFile().", line ".
         $e->getLine()."): ".$e->getMessage();

} ?>

 </source>
   
  


fopen() function opens a file, returns an integer, as a file handle.

   <source lang="html4strict">

Its syntax is: int fopen (string file, string mode [, int use_include_path]) The mode specifies the read/write readiness of the file. MODE MEANING r Read only. r+ Reading and writing. w Write only. w+ Reading and writing. a Write only. a+ Reading and writing.

If use_include_path is set to 1, file path is compared to the include path contained in the php.ini file. <?

   $file = "data.txt";                                             // some file
   $fh = fopen($file, "a+") or die("File ($file) does not exist!");

?>

 </source>
   
  


fopen() requires the file path and the mode in which the file is to be opened.

   <source lang="html4strict">

The most common modes are read ("r"), write ("w"), and append ("a"). fopen() returns a file resource you will later use to work with the open file. To open a file for reading, you would use the following: $fp = fopen( "test.txt", "r" );

You would use the following to open a file for writing:

$fp = fopen( "test.txt", "w" );

To open a file for appending, you would use this:

$fp = fopen( "test.txt", "a" );

 </source>
   
  


fopen() returns false if the file cannot be opened for any reason.

   <source lang="html4strict">

if ( $fp = fopen( "test.txt", "w" ) ) {

 // do something with $fp

} Or you can use a logical operator to end execution if an essential file can"t be opened:

( $fp = fopen( "test.txt", "w" ) ) or die ("Couldn"t open file, sorry");

 </source>
   
  


Getting and Printing a Web Page with fopen()

   <source lang="html4strict">

<html> <head> <title>Getting a Web Page with fopen()</title> </head> <body>

<?php $webpage = "http://www.wbex.ru/index.htm"; $fp = fopen( $webpage, "r" ) or die("couldn"t open $webpage"); while ( ! feof( $fp )) {

 print fgets( $fp, 1024 );

} ?>

</body> </html>

 </source>
   
  


Getting and Putting Files with FTP

   <source lang="html4strict">

<?php $file_name = "somefile.ext"; $fp = fopen($file_name, "rb"); if ($fp) {

 $data = fread($fp, filesize($file_name));
 fclose($fp);

} ?>

 </source>
   
  


If you are writing a binary file on a Windows system, you should add a "b" flag.

   <source lang="html4strict">

$fp = fopen( "binary_file", "wb" ); and read them like this: $fp = fopen( "binary_file", "rb" );

 </source>
   
  


Load remote file

   <source lang="html4strict">

<?php $file_name = "http://php.net/index.php"; $fp = fopen ( $file_name, "r" ); if ($fp) {

 $data = "";
 
 while ( ! feof ( $fp ) ) {
   
   $data .= fread ( $fp, 4096 );
 
 }
 
 fclose ( $fp );

} echo $data; ?>

 </source>
   
  


Open a connection with the PHP site (http://www.php.net):

   <source lang="html4strict">

<?

   $site = "http://www.php.net";      // some server that can communicate via HTTP
   $sh = fopen($site, "r");           // assigns PHP.net index page to a filehandle.

?>

 </source>
   
  


Opening a file

   <source lang="html4strict">

<?php $fh = fopen("file.txt","r") or die("can"t open file.txt: $php_errormsg"); ?>

 </source>
   
  


Opening a file on Windows

   <source lang="html4strict">

$fh = fopen("c:/settings.txt","rb");

 </source>
   
  


Opening a remote file

   <source lang="html4strict">

<?php $fh = fopen("http://www.example.ru/robots.txt","r") or die($php_errormsg); ?>

 </source>
   
  


Opening Files

   <source lang="html4strict">

<?php $file = "data.txt"; if (file_exists ($file)){

   try { 
       if ($readfile = fopen ($file, "r")){ 
           echo "File opened successfully."; 
       } else { 
           throw new exception ("Sorry, the file could not be opened."); 
       } 
   } catch (exception $e) { 
       echo $e->getmessage(); 
   } 

} else {

   echo "File does not exist."; 

} ?>

 </source>
   
  


Opening files in the include_path

   <source lang="html4strict">

<?php $fh = fopen("file.inc","r",true) or die("can"t open file.inc: $php_errormsg"); ?>

 </source>
   
  


PHP 5 Arguments for Opening a File

   <source lang="html4strict">

Argument Description r Opens a file for reading r+ Opens a file for both reading and writing w Opens a file for writing only w+ Opens a file for reading and writing a Opens a file for appending (write-only) a+ Opens a file for appending (read/write) X Creates a file and opens it for writing only x+ Creates a file and opens it for reading and writing

<?php $file = "data.txt"; if (file_exists ($file)){

   try { 
       if ($readfile = fopen ($file, "r")){ 
           $curvalue = fread ($readfile,100); 
           echo $curvalue; 
       } else { 
           throw new exception ("the file could not be opened."); 
       } 
   } catch (exception $e) { 
       echo $e->getmessage(); 
   } 

} else {

   echo "File does not exist."; 

} ?>

 </source>
   
  


Reading a File with fread()

   <source lang="html4strict">

<html> <head> <title>Reading a File with fread()</title> </head> <body>

<?php $filename = "data.txt"; $fp = fopen( $filename, "r" ) or die("Couldn"t open $filename"); while ( ! feof( $fp ) ) {

 $chunk = fread( $fp,16 );
 print "$chunk
";

} ?>

</body> </html>

 </source>
   
  


Reading from standard input

   <source lang="html4strict">

<?php $fh = fopen("php://stdin","r") or die($php_errormsg); while($s = fgets($fh)) {

   print "You typed: $s";

} ?>

 </source>
   
  


Safely reading a binary file

   <source lang="html4strict">

<?php $fh = fopen("c:/images/logo.gif","rb"); ?>

 </source>
   
  


Using fopen( )

   <source lang="html4strict">

<?

   $fp = fsockopen ("data.org", 80);
   if ($fp) {
           fwrite($fp, "GET / HTTP/1.1\r\nHOST: data.org\r\n
                   ACCEPT-ENCODING: gzip\r\n\r\n");
           while (!feof($fp)) {
                   print fread($fp,256);
           }
           fclose ($fp);
   } else {
           print "Fatal error\n";
   }

?>

 </source>
   
  


Using the fopen() Function

   <source lang="html4strict">

<?php

    $fr = fopen("myfile.txt", "r");
    $fr = fopen("myfile.dat", "ba+");
    $fr = fopen("code.php", "w+", true);
    $fr = fopen("http://www.php.net/index.php", "r");
    $fr = fopen("ftp://ftp.php.net/index.php", "r");
    $url = "http://www.php.net/this is my invalid URL.php";
    $url = urlencode($url);
    $fr = fopen($url, "r");

?>

 </source>