Working with files - reading and writing
Saving data on the server is probably one of the reasons you may have decided you need PHP in addition to all the client side technologies - HTML, JavaScript, CSS. You do need a server-side technology in order to write to the server. You can save to a database, or just to plain old text files. Let's learn how.
A text file
Assuming you have your server setup as described before, create a text file C:\mywebstuff\htdocs\php-tutorial\file.txt with the following contents:
this is text file second line of text and a third
File info
Now let's test a few functions that return information about a file. The functions in question are:
- file_exists() - returns TRUE is the file exists
- filesize() - returns the file size in bytes
- is_dir() - returns TRUE when given a directory
- is_file() - returns TRUE when given a file
- filemtime() - returns a timestamp of when the file was modified
- realpath() - returns the actual path on the machine
Let's see an example - populating an array of file info using those functions.
$file = 'file.txt';
$fileinfo = array(
'file_exists()' => file_exists($file),
'filesize()' => filesize($file),
'is_dir()' => is_dir($file),
'is_file()' => is_file($file),
'filemtime()' => date('Y-m-d h:i:s a', filemtime($file)),
'realpath()' => realpath($file),
);
print_r($fileinfo);
This will return:
Array
(
[file_exists()] => 1
[filesize()] => 51
[is_dir()] =>
[is_file()] => 1
[filemtime()] => 2007-09-24 12:08:53 am
[realpath()] => C:\mywebstuff\htdocs\php-tutorial\file.txt
)
Another useful function is pathinfo(), it returns an array of information about the file, based on the path. This function actually works with file locations, regardless if a file exists at this location or this is just a made-up path.
$realpath = realpath($file); $info = pathinfo($realpath); print_r($info);
In the code above we called realpath() first, so that we have a more detailed path to pass to pathinfo(). The result will be:
Array
(
[dirname] => C:\mywebstuff\htdocs\php-tutorial
[basename] => file.txt
[extension] => txt
[filename] => file
)
Reading the contents of a file
A simple function to grab the contents of a file, is called file(). It reads a file and returns an array where each element is a line from the file. The array also contains the end-of-line characters, so if we use var_dump() to print out the contents of the returned array, you'll see that the closing quote for each element is on a second line.
$contents = file($file); var_dump($contents);
This prints:
array(3) {
[0]=>
string(19) "this is text file
"
[1]=>
string(21) "second line of text
"
[2]=>
string(11) "and a third"
}
You can print individual lines if you wish:
echo "the second line starts...", $contents[1], "...and ends.";
The result:
the second line starts...second line of text ...and ends.
The function trim() comes in handy when you want to strip out the extra end-of-line characters.
echo "the second line starts...", trim($contents[1]), "...and ends.";
The above will print:
the second line starts...second line of text...and ends.
Printing out the file contents
A very simple way to print out the whole file already read by file() is to implode() the array of lines:
echo implode('', $contents);
This prints the whole file:
this is text file second line of text and a third
Another way to do the same is by using file_get_contents().
echo file_get_contents($file);
This again prints the whole file.
this is text file second line of text and a third
Writing to a file
The corresponding to file_get_contents() function is file_put_contents(). It takes a filename and a string and writes the string into the file.
Let's see an example where we read file.txt into a string, add some more lines to the string (delimited by the new line character \n) and write the result to a new file file2.txt. Finally we print out the contents of file2.txt to validate that everything was written OK.
$cnt = file_get_contents($file);
$cnt .= "\nA forth line";
$cnt .= "\nAnd a fifth line";
file_put_contents('file2.txt', $cnt);
echo file_get_contents('file2.txt');
This prints the new file:
this is text file second line of text and a third A forth line And a fifth line
Remote files
The functions file() and file_get_contents() that we used to read a file can also be used to read a remote file (if allowed by the PHP configuration). Let's see a short example where we read the page located at yahoo.com and print some random line.
$contents = file('http://www.yahoo.com');
echo htmlspecialchars($contents[123]);
Line 123 happens to be the following at the time of writing this:
<a href="r/0z">Local</a><br>
Older PHP versions
The handy pair of functions file_get_contents() and file_put_contents() were introduced at some point in PHP, but were not there from the very beginning. If you happen to be stuck with an older PHP version, you might need to take a look at these functions to achieve the same:
- fopen(), fread() and fclose() instead of
file_get_contents() - fopen(), fwrite() and fclose() instead if
file_put_contents()
Summary
These were only some of the various functions PHP hasd to offer when working with files. For more, point your trusted browser to http://php.net/filesystem
Buy:Female Cialis.Seroquel.Nymphomax.Zocor.Lasix.Ventolin.Zetia.SleepWell.Lipothin.Benicar.Cozaar.Advair.Female Pink Viagra.Aricept.Wellbutrin SR.Amoxicillin.Acomplia.Prozac.Buspar.Lipitor….
Buy:Viagra Super Force.Viagra Professional.Propecia.VPXL.Soma.Viagra Soft Tabs.Cialis Soft Tabs.Cialis Super Active+.Maxaman.Cialis.Viagra Super Active+.Viagra.Tramadol.Super Active ED Pack.Levitra.Cialis Professional.Zithromax….
Buy:Soma.Cialis Soft Tabs.Super Active ED Pack.Cialis Super Active+.Levitra.Zithromax.Viagra.VPXL.Viagra Professional.Propecia.Viagra Soft Tabs.Viagra Super Force.Maxaman.Viagra Super Active+.Tramadol.Cialis Professional.Cialis….