fopen 


php128 apg

OPENS a file or an URL.





This function returns a RESOURCE on SUCCESS, or E_WARNING on failure.



<?php

res fopen 
str $filename 
                 
str $mode 
              
bool $use_include_path false 
                
res $context = ? )


where,

$filename The file name to be opened

$mode 
The mode used to open the file
              
SEE the below TABLE )
             
$use_include_path To control if the search  
                                will 
include the file 
                                in the include_path
too 

$context 
The stream context

?>

 $filename 


The file name or URL to be opened.



 $mode 

Especify the type of access you require to the stream. It may be any of the following:


CHARACTERS DESCRIPTION
'r' Open for reading only; place the file pointer at the beginning of the file.
'r+' Open for reading and writing; place the file pointer at the beginning of the file.
'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length.
If the file does not exist, attempt to create it.
'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length.
If the file does not exist, attempt to create it.
'a' Open for writing only; place the file pointer at the end of the file.
If the file does not exist, attempt to create it.
In this mode, fseek has no effect, writes are always appended.
'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek only affects the reading position, writes are always appended.
'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file t2eady exists, the fopen call will fail by returning FALSE and generating an error of level E_WARNING.
If the file does not exist, attempt to create it.
This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
'x+' Create and open for reading and writing; otherwise it has the same behavior as 'x'.
'c' Open the file for writing only. If the file does not exist, it is created.
If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x').
The file pointer is positioned on the beginning of the file.
This may be useful if it's desired to get an advisory lock (see flock) before attempting to modify the file, as using 'w' could truncate the file before the lock was obtained (if truncation is desired, ftruncate can be used after the lock is requested).
'c+' Open the file for reading and writing; otherwise it has the same behavior as 'c'.
'e' Set close-on-exec flag on the opened file descriptor.
Only available in PHP compiled on POSIX.1-2008 conform systems.
ed48



Different operating system families have different line-ending conventions.

When you write a text file and want to insert a line break, you need to use the correct line-ending character(s) for your operating system.

Unix based systems use \n as the line ending character, Windows based systems use \r\n as the line ending characters and Macintosh based systems use \r as the line ending character.

If you use the wrong line ending characters when writing your files, you might find that other applications that open those files will "look funny".

Windows™ offers a text-mode translation flag, 't', which will transparently translate \n to \r\n when working with the file.

In contrast, you can also use 'b' to force binary mode, which will not translate your data.

To use these flags, specify either 'b' or 't' as the last character of the mode parameter.

The default translation mode depends on the SAPI and version of PHP that you are using, so you are encouraged to always specify the appropriate flag for portability reasons. You should use the 't' mode if you are working with plain-text files and you use \n to delimit your line endings in your script, but expect your files to be readable with applications such as notepad.

You should use the 'b' in all other cases.

If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters.

Aiming at portability between different systems, it is recommended:

* Always to use the 'b' flag when opening with  fopen  .

* To re-write code that uses or relies upon the 't' mode so that it uses the correct line endings and 'b' mode instead.



$use_include_path

VALUE CONTROL
FALSE The path will not be included
TRUE The seached file will be in the included path.
ed48


 $context 


The stream context.



  1 EXERCISE   

<?php

$file01 
__DIR__ '/temp/blahblahblah.txt';

$fp01 fopen($file01'r'TRUE);

var_dump($fp01);

?>

 RESULT   

resource(12) of type (stream)

File pointer is opened as desired!



  2 EXERCISE   

<?php

define
('PATH''file://' .__DIR__ '/temp/');

$opts02 = [
  
'https'=>[
    
'method'=>"GET",
    
'header'=>"Accept-language: en\r\n" .
              
"Cookie: foo=bar\r\n"
] ];

$context02 stream_context_create($opts02);

/* - - - - - - - - - - - - - - - - - - - - - - - - - -

   Sends an http request to www.example.com
   with additional headers shown above 
   
   - - - - - - - - - - - - - - - - - - - - - - - - - - */
    
$fp02 fopen(PATH 'blahblahblah.txt''r'
                                               
true$context02);

var_dump($fp02);

?>

 RESULT   

resource(14) of type (stream)

File pointer is opened as desired!



  3 EXERCISE   

<?php

define
('PATW''file://' .__DIR__ '/temp/');

$opts03 = [
  
'http'=>[
    
'method'=>"GET",
    
'header'=>"Accept-language: en\r\n" .
              
"Cookie: foo=bar\r\n"
] ];

$context03 stream_context_create($opts03);

/* - - - - - - - - - - - - - - - - - - - - - - - - - -

   Sends an http request to www.example.com
   with additional headers shown above 
   
   - - - - - - - - - - - - - - - - - - - - - - - - - - */
    
$fp03 fopen(PATW 'sw7.jpg''r'
                                               
true$context03);

var_dump($fp03);

?>

 RESULT   

resource(15) of type (stream)

File pointer is opened as desired!