I find it interesting that no one else has commented on this lesson yet.
My file open code is here
it is a functioning example!
This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.
Being able to create, open, read, write, upload, edit and delete files can be very important depending on the application you're going to create. All of these are doable in PHP!
This can be somewhat complicated to some people, so I'm going to write one class for each ability. Starting with...
To open a file, we use the fopen() function.
fopen($filename, $mode)
$filename refers to where the file being opened is.
$mode is how it reads the file.
Mode | Action (php.net) |
---|---|
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. " |
x | " Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it..." |
x+ | " Create and open for reading and writing; otherwise it has the same behavior as 'x'. " |
c | " Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file... " |
c+ | " Open the file for reading and writing; otherwise it has the same behavior as 'c'. " |
Resource from php.net
Lets look some sample code:
<?
$open = fopen("test.html", "r") or die("Cannot open, does not exist");
?>
We used the fopen() function, with the "r" mode, which will just open the file. If you try this, you should get an error like this:
Warning: fopen(test.html) [function.fopen]: failed to open stream: No such file or directory in /directory/path/fopen.php on line 2
Cannot open, does not exist
We followed the $open variable with a new function called die().
die($string) or exit($string) (They are the same)
$string can be void; die()/exit() is allowed!
Will stop the script from running anything below it.
If $string is assigned, it will output your string as the last command your page will run.
VERY USEFUL FOR DEBUGGING
The die() and exit() functions can't be used on everything. fopen() is one that it can be used on!
Anyways, we got that error because test.html does not exist. Create a file called test.html and put it in the same directory as fopen.php, now run the same script.
Nothing shows up? Good! That means PHP was able to find, and read test.html without any problem. But it still doesn't show up the content of test.html.
This is the function we use to get the content of a file:
fgets($handle, $length)
$handle is the term we use for opening the file. Earlier, the $open variable was our handle.
$length is an integer, optional, and measued in bytes. 1 character = 1 byte (fairly decent rule of thumb).
To continue on from our earlier example, I'll show you how to actually read the contents of the file you want to open.
<?
$open = fopen("test.html", "r") or die("test.html does not exist");
$read = fread($open, 10);
fclose($open);
echo $read;?>
Here's what we did:
- Assigned $open as the handler, to read test.html
- Assigned $read to read test.html, but only the first 10 bytes of it
- Closed the file using fclose($handle)
- echo'd the contents of $read (Which is test.html)
So as you see, it's not too difficult. I would like to point out though that if you have any markup, such as HTML, it will display it as HTML and not as regular text.
fclose($handle)
$handle being the variable assigned with fopen()
Should be used anytime your done with a file. If fclose() is not called, the file is closed at the end of the script.
As a good practise, always close your files.
Your task:
-Create a new PHP file called "fopen.php" and add the code to open "readme.txt".
-Create readme.txt, and add atleast 20 characters (1 space = 1 character as well)
-Run fopen.php. If it works, change the code in fopen.php to only read the first 10 characters.
Any questions, please click the "comment" button above this article!