PHP Filesystem

the php fopen() method is used for open files.

the fclose() method is used to close an open files in php.


what is a file ?

files are stored in directories on a hard drive, and because they retain their data after the computer is shut down, they are a persistent storage mechanism, instead of temporary storage such as ram. a file can contain any kind of data.


PHP - opening a file

the fopen() function is used to opens a file and returns a file handle associated with the file.

if the fopen() function is unable to open the specified file, it returns false( 0 ).

syntax:

         fopen( filename, mode )
  • filename: specifies the name of the file you want to open.
  • mode    : specifies the mode, how the file is to be used.

note : a remote file can only be opened for reading, you can't write to the file.

      <html>
      
         <head> <title>opening files </title></head>
         
         <body> <?php
                            $open =  fopen( "http://localhost:80/data.html", "r" );
                                  
                            $xx    =    fopen( ".\\show.php", "r" );  
                                  
                      ?>
      </body>
         
 </html>

the file may be opened in one of the following modes:

modedescription
ropen for reading only. the file pointer at the beginning of the file
r+read/write. the file pointer at the beginning of the file
wfile open for writing only. w mode use to creates a new file if it doesn't exist
w+read/write.w+ mode use to creates a new file if it doesn't exist
aappend. opens and writes to the end of the file
a+read/append. preserves file content by writing to the end of the file
xwrite only. creates a new file. returns false and an error if file already exists
x+write/read. creates a new file. returns false and an error if file already exists

note :- the file pointer is php internal pointer that specifies the exact character position in a file where the next operation should be performed.


PHP - closing a file

the php fclose() method is used to close an open file.

the fclose() function return true on success or false on failure.

     <?php     $file =  fopen( "e:\\project\users\sandeep.txt", 'w+')

                            fclose( $file ); //remore file handle from memory
         ?>

PHP - File paths

absolute path - begin with a (/ on window platform) (\ on linux platform), indicating that the path is relative to the root of the file system, not to the current directory.

      /apache/bin/demo.exe;  c:/window/web/data.php     //absolute paths

relative path:

  • within the relative path, a dot ( . ) refers to the current directory.
  • within the relative path, two dots ( .. ) refer to the immediate parent directory.
     ./data.html       this path points  to a file called data.html in the current directory.
     ../data.html    this path points to a file called data.html in the directory above the current directory
../../../data.html  this path backs up the directory tree three level before looking for the data.html file