|
Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index
PHP Script Tips - Reading and Writing Files
By: FYICenter.com
Part:
1
2
3
4
A collection of 14 tips on PHP functions on file input and output. Clear answers are provided with tutorial exercises on opening files for reading and writing, appending data to existing files, reading file by line or character, reading file in binary mode.
Topics included in this collection are:
- How To Read a Text File into an Array?
- How To Read the Entire File into a Single String?
- How To Open a File for Reading?
- How To Open a File for Writing?
- How To Append New Data to the End of a File?
- How To Read One Line of Text from a File?
- How To Read One Character from a File?
- What's Wrong with "while ($c=fgetc($f)) {}"?
- How To Read a File in Binary Mode?
- How To Write a String to a File with a File Handle?
- How To Write a String to a File without a File Handle?
- How To Write an Array to a File without a File Handle?
- How To Read Data from Keyborad (Standard Input)?
- How To Open Standard Output as a File Handle?
How To Read a Text File into an Array?
If you have a text file with multiple lines, and you want to read those lines
into an array, you can use the file() function. It opens the specified file, reads all the lines,
puts each line as a value in an array, and returns the array to you.
Here is a PHP script example on how to use file():
<?php
$lines = file("/windows/system32/drivers/etc/services");
foreach ($lines as $line) {
$line = rtrim($line);
print("$line\n");
# more statements...
}
?>
This script will print:
# This file contains port numbers for well-known services
echo 7/tcp
ftp 21/tcp
telnet 23/tcp
smtp 25/tcp
...
Note that file() breaks lines right after the new line character "\n".
Each line will contain the "\n" at the end. This is why we suggested
to use rtrime() to remove "\n".
Also note that, if you are on Unix system, your Internet service file
is located at "/etc/services".
How To Read the Entire File into a Single String?
If you have a file, and you want to read the entire file into a single string,
you can use the file_get_contents() function. It opens the specified file,
reads all characters in the file, and returns them in a single string.
Here is a PHP script example on how to file_get_contents():
<?php
$file = file_get_contents("/windows/system32/drivers/etc/services");
print("Size of the file: ".strlen($file)."\n");
?>
This script will print:
Size of the file: 7116
How To Open a File for Reading?
If you want to open a file and read its contents piece by piece,
you can use the fopen($fileName, "r") function. It opens the specified file,
and returns a file handle. The second argument "r" tells PHP to open
the file for reading. Once the file is open, you can use other functions to read
data from the file through this file handle.
Here is a PHP script example on how to use fopen() for reading:
<?php
$file = fopen("/windows/system32/drivers/etc/hosts", "r");
print("Type of file handle: " . gettype($file) . "\n");
print("The first line from the file handle: " . fgets($file));
fclose($file);
?>
This script will print:
Type of file handle: resource
The first line from the file handle: # Copyright (c) 1993-1999
Note that you should always call fclose() to close the opened file
when you are done with the file.
How To Open a File for Writing?
If you want to open a new file and write date to the file,
you can use the fopen($fileName, "w") function. It creates the specified file,
and returns a file handle. The second argument "w" tells PHP to open
the file for writing. Once the file is open, you can use other functions to write
data to the file through this file handle.
Here is a PHP script example on how to use fopen() for writing:
<?php
$file = fopen("/temp/todo.txt", "w");
fwrite($file,"Download PHP scripts at dev.fyicenter.com.\r\n");
fclose($file);
?>
This script will write the following to the file:
Download PHP scripts at dev.fyicenter.com.
Note that you should use "\r\n" to terminate lines on Windows.
On a Unix system, you should use "\n".
(Continued on next part...)
Part:
1
2
3
4
|