To open a file or a URL we use fopen().
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.
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().
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.
So now $contents would contain:
$contents = "line1nline2nline3nline4nline5";
If we printed the variable out the output would be:
The contents of the file are read into a
string, complete with newline characters (n). We can then process this
string however we like.
We can also write data into a file once we have opened it. To do this we use the fputs() function.
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.
I think you'll agree they've made it very easy to handle files in PHP.
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 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.
Two very useful functions in PHP are include()
and require(). These functions do what they say. They allow you to
include other files into a PHP document. Require() and Include() behave
slightly differently, but are basically the same.
This will be the header of your page.
This will be the footer of your page.
Now we have two files containing some HTML and
some PHP code. If we're making many pages, why do this in every file?
We could just include these two files into each page, making documents
smaller and simpler. This is how we would do it:
This would simply include page_header.php at
the top of the file, and page_footer.php at the bottom of the file. We
wouldn't need to write out the code everytime, just include it. An
included file can itself contain an include() function, and PHP will
handle it with ease. Always try to use the full system path to the file
you're including, not the relative path, this will ensure PHP can find
the files easily. In the above example we simply include the header and
footer PHP documents and print out a few variables. We also use the
foreach() function to go through each value in the $members array and
print the members out in a neat little table.
In the above example we could use require() and it would have the same effect, so what's the difference between them?
One difference between require() and include()
is the way they handle an error. If PHP cannot find the file you have
entered in the include() function, it just prints out a warning and
informs you that the file was not included, yet the script continues.
If the same happens with require(), it doesn't issue a warning, it
causes a fatal error meaning that if the require function doesn't
return true PHP will stop executing the script there and then and tell
As it says on the PHP reference page: "In other
words, use require() if you want a missing file to halt processing of
the page. include() does not behave this way, the script will continue
© 2004-2014 Nova Holding Group LTD. - articles and tutorials. Website maintained by Maestro Solutions LTD. This website designed by Templatemonster.com