PHP More Functions
This section describes file handling in PHP.
Opening a File
The fopen() function is used to open files in PHP.
The first parameter of this function contains the name of the file to be opened and the
second parameter specifies in which mode the file should be opened in:
<html>
<body>
<?php
$f=fopen("welcome.txt","r");
?>
</body>
</html>
|
The file may be opened in one of the following modes:
| File Modes |
Description |
| r |
Read only. File pointer at the start of the file |
| r+ |
Read/Write. File pointer at the start of the file |
| w |
Write only. Truncates the file (overwriting it). If the
file doesn't exist, fopen() will try to create the file |
| w+ |
Read/Write. Truncates the file (overwriting it). If the
file doesn't exist, fopen() will try to create the file |
| a |
Append. File pointer at the end of the file. If the file
doesn't exist, fopen() will try to create the file |
| a+ |
Read/Append. File pointer at the end of the file. If the file
doesn't exist, fopen() will try to create the file |
| x |
Create and open for write only. File pointer at the
beginning of the file. If the file already exists, the fopen() call will
fail and generate an error. If the file does not exist, try to create it |
| x+ |
Create and open for read/write. File pointer at the
beginning of the file. If the file already exists, the fopen() call will
fail and generate an error. If the file does not exist, try to create it |
Note: If the fopen() function is unable to open the
specified file, it returns 0 (false).
Example
The following example generates a message if the fopen() function is unable
to open the specified file:
<html>
<body>
<?php
if (!($f=fopen("welcome.txt","r")))
exit("Unable to open file!");
?>
</body>
</html>
|
Closing a File
The fclose() function is used to close a file.
Reading from a File
The feof() function
is used to determine if the end of file is true.
Note: You cannot read from files opened in w, a, and x mode!
if (feof($f))
echo "End of file";
|
Reading a Character
The fgetc() function is used to read a single character from a file.
Note: After a call to this function the file pointer has moved to the next character.
Example
The example below reads a file character by character, until the end of
file is true:
<?php
if (!($f=fopen("welcome.txt","r")))
exit("Unable to open file.");
while (!feof($f))
{
$x=fgetc($f);
echo $x;
}
fclose($f);
?>
|
Reliable, Affordable, Feature-Rich Web Hosting!
Take the uncertainty out of Web hosting and let
GoDaddy.com put service, performance and value back in. No matter which
hosting type or plan you choose, your site receives 24/7
maintenance and protection in our world-class data center. Plus,
you get the expert, friendly service you deserve, from the
world's largest hostname provider.
With three plans to choose from and
prices starting at just $4.99 per month, GoDaddy.com is sure to have a plan that's
right-sized and right-priced just for you!
All plans feature FREE 24x7 setup, FREE 24x7 monitoring, best-
of-breed routers, firewalls and servers, 24x7 onsite physical security
and access to our exclusive Go Daddy Hosting Connection, THE place
to install over 30 FREE applications. Virtual Dedicated and Dedicated
Server plans also available.
Visit GoDaddy.com today.
Virtual Dedicated, Dedicated Server and unlimited plans also available.
Save 20% on 12 months or more of shared web hosting - Enter code w3s20off at checkout
|