We can use
PHP to open files and
manipulate the data
contained within them.
Opening a File
To open a
file or a URL we use fopen().
$filename =
'c:\file.txt';
$fp =
fopen($filename,
"r"); |
|
The fopen()
function normally uses two
parameters. The first is the
file which we want to open.
This can be a file on your
system, or a file on a
remote server. In this
example we are opening a
text file on a windows
machine. The second
parameter is the "mode". We
can open a file for only
reading. We can open a file
for only writing. We can
open a file for reading and
writing. In the above
example "r" means reading.
Reading Info from a File
After we have
opened the file, we will
probably want to do
something with it. To read
the contents of a file into
a variable we use fread().
$contents =
fread($fp,
filesize($filename));
fclose($fp); |
|
Now fread()
also uses two parameters.
The first parameter is the
variable to which we
assigned the fopen()
function, the second is the
number of bytes we want to
read up to in the file. In
this case we want to read
the entire file, so we use
the filesize() function to
get the size of the file
specified in the $filename
variable. Thus, it reads the
entire file in.
Let us assume C:file.txt
contains:
line1
line2
line3
line4
line5 |
|
So now
$contents would contain:
$contents = "line1\n line2\n
line3\n line4\n line5";
If we printed the variable
out the output would be:
line1
line2
line3
line4
line5 |
|
The contents
of the file are read into a
string, complete with
newline characters (\n. We
can then process this string
however we like.
Writing To a File
We can also
write data into a file once
we have opened it. To do
this we use the fputs()
function.
$filename =
'c:\file.txt';
$fp =
fopen($filename,
"a");
$string =
"\nline5";
$write =
fputs($fp,
$string);
fclose($fp); |
|
Firstly we
open the file. Notice the
"a" parameter? That means
"open the file for writing
only, and place the file
pointer at the end of the
file". So now PHP opens
myfile.txt for writing and
positions the file pointer
at the end of the line.
We then use fputs() to write
to the file. We define our
string in $string which i
put "\nline6. The \n bit
means start a new line, and
the print out "line 6". We
then close the file.
So now what will the file
contain?
line1
line2
line3
line4
line5 |
|
Modes
There are
various modes you can use to
open a file, they are listed
on the php.net fopen()
function page, but i'll
stick them up on here too.
-
'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.
-
'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.
The above was
taken from php.net's fopen()
function reference page. |