- PHP intro
- PHP tags
- PHP Comments
- PHP Echo Print
- PHP Variables Types
- PHP Variable Scope
- PHP Data Type
- PHP type casting
- PHP Operator
- Comparison Operator
- Operator Precedence
- PHP Constant
- PHP If Else if
- PHP Switch Case
- PHP While loop
- PHP For loop
- PHP Break/Continue
- PHP Foreach loop
- PHP String
- PHP Array
- PHP Multidimens array
- PHP Function
- PHP Global Variables
- PHP Code Rules
- PHP Include
- PHP Date/Time
- PHP Files
- PHP File read/write
- PHP File Upload
- PHP Images
- PHP Cookie
- PHP Session
- PHP Filter
- PHP Error Handling
- PHP Exceptions
- PHP Oops
- PHP Class Methods
- PHP Abstract classes
- PHP Final Classes
- PHP Interface
- PHP Inheritance
- PHP Overriding
- PHP Object Serialize
- PHP Mysql Demo
- PHP Mysql Connection
- PHP Database/Table
- PHP Mysql Select
- PHP Mysql Insert
- PHP Mysql Update
- PHp Mysql Delete
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 ).
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:
|r||open for reading only. the file pointer at the beginning of the file|
|r+||read/write. the file pointer at the beginning of the file|
|w||file 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|
|a||append. opens and writes to the end of the file|
|a+||read/append. preserves file content by writing to the end of the file|
|x||write 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 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
- 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