PHP Files Creating or Opening a Local File - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Files Creating or Opening a Local File - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Tuesday, July 9, 2019

PHP Files Creating or Opening a Local File

PHP Files 


Creating or Opening a Local File

Problem

You want to open a local file to read data from it or write data to it.

Solution

Use fopen():

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

Discussion

The first argument to fopen() is the file to open; the second argument is the mode in which to open the file. The mode specifies what operations can be performed on the file (reading and/or writing), where the file pointer is placed after the file is opened (at the beginning or end of the file), whether the file is truncated to zero length after opening, and whether the file is created if it doesn’t exist.

Table  fopen() file modes

Mode    Readable?    Writable?    File pointer    Truncate?    Create?    
r             Yes                 No             Beginning       No                 No
r+           Yes                 Yes            Beginning       No                 No
w            No                 Yes             Beginning      Yes                Yes
w+         Yes                 Yes             Beginning      Yes                Yes
a             No                 Yes             End                 No                Yes
a+          Yes                 Yes             End                 No                Yes
x             No                 Yes             Beginning       No                Yes
x+          Yes                 Yes             Beginning       No                Yes
________________________________________________________

The x and x+ modes return false and generate a warning if the file already exists. On non-POSIX systems, such as Windows, you need to add a b to the mode when opening a binary file, or reads and writes get tripped up on NUL (ASCII 0) characters. For instance:

       $fh = fopen('c:/images/logo.gif','rb');

Even though Unix systems handle binary files fine without the b in the mode, it’s a good idea to use it always. That way, your code is maximally portable and runs well on both Unix and Windows.

To operate on a file, pass the filehandle returned from fopen() to other I/O functions such as fgets(), fputs(), and fclose().

If the file given to fopen() doesn’t have a pathname, the file is opened in the directory of the running script (web context) or in the current directory (command-line context).

You can also tell fopen() to search for the file to open in the include_path specified in your php.ini file by passing true as a third argument. This example searches for file.inc in the include_path:

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

No comments:

Post a Comment

Post Top Ad