
| Clearing up some HDD space in windows vista and windows XP ** | Setting up a Windows Web Server with PHP and MySQL |
| Submitted On: Saturday, August 1, 2009 at 9:12:18 PM |
Creeper Please login to rate this submission. Link to this Submission Blog and Forum Link HTML link Facebook / Pagereaders
|
DO NOT ATTEMPT: Articles provided here are for INFORMATIONAL PURPOSES ONLY. DO NOT undertake any project based upon any information obtained on the internet, including this website. We are not responsible for, nor do we assume any liability for, damages resulting from the use of any information on this site. Please read the Legal page for more information. Functions you'll use: - fopen() - Opens the file. - fgets() - Reads a line from the file. - fwrite() - Writes a line to the file. - fclose() - Closes the file. - feof() - Checks for the end of a file. - file_exists() - Checks if the file exists. - file_get_contents() - Returns the entire file. There are several uses for these functions. You can open files on your server, or even open a web page on a different server (only for reading), if your php.ini is configured correctly. First, I'll go over the fopen() (file open) function: $file = fopen("myfile.txt","r"); Here's a breakdown of the above: $file -- This variable is the file's "handle." Using this variable, other functions (fgets, fwrite, etc) will know what file that the function should work with. "myfile.txt" -- This is the file you're working with, assuming it is in the same directory as the PHP script is. "r" -- This tells PHP that you want to open this file to be READ. Other modes are: "w" for write, and "a" for append. There are several others, but I'll leave those for a different article. But this assumes that the file exists in the first place. If the file doesn't exist, you'll soon find out you're getting an error (unless you're using the write or append modes.) How to check if the file exists: if (file_exists("myfile.txt")) { $file = fopen("myfile.txt","r"); } Of course, I'm assuming you understand some programming / PHP before hand. This function is pretty self explanatory. Opening a file to read it, then close it: <?PHP if (file_exists("myfile.txt")) { $file = fopen("myfile.txt","r"); $data = fgets($file); echo $data; fclose($file); } else { echo "File doesn't exist!"; } ?> You'll notice that this will either output "File doesn't exist", or the FIRST LINE of the file. If you got the error, make the file, and put some random info in it. Here's the breakdown of the functions I used above: fgets: $data - This is the variable that stores the data that fgets() read from the file. $file - Again, this is the file handle. This tells fgets() that it should open "myfile.txt" for "r" (reading), which it knows from the fopen() function. fclose: $file - Closes the file handle that you opened earlier with fopen(). Why do we need file handles? You may run into the occasional requirement to work with two (or more) files at one time. You may want to read a file, work with the data, and output it to a different file. If this is the case, you'll want to give each file a different...
|
||||
| Tags: PHP, File, Function, Basics, fopen, fclose, fwrite, fgets, file_get_contents, file_exists, feof, Programming |
| Saturday, August 1, 2009 at 10:33:17 PM #21439 |
|
teh1337 $Pedo++;
Level 25 Posts: 1,365 Submissions: 71 YeN: 11,119,189 |
I hit the 0* by accident. Oh wait, it was intentional. Good article though. |
![]() |
| Sunday, August 2, 2009 at 4:18:40 AM #21454 |
|
DeadLazyBum Site Admin '> Creeper's Title'
Level 29 Posts: 1,868 Submissions: 121 YeN: 224,147,486 |
5* |
| Thursday, August 27, 2009 at 10:21:53 PM #22062 |
| Phil Removed Account
|
Good guide, Will definitely need this soon. ![]() |