Week 5 a Managing Files Using PHP
Week 5 a Managing Files Using PHP
Creating a file
At this point we assume testfile.txt doesn’t exist, so let’s
create it and write a few lines to it
Example:
<?php // testfile.php
$fh = fopen("testfile.txt", 'w') or die("Failed to create file");
$text = <<<_END
Line 1
Line 2
Line 3
_END;
fwrite($fh, $text) or die("Could not write to file");
fclose($fh);
echo "File 'testfile.txt' written successfully";
?>
Writing string into a File
To write a string to a file, we can use file_put_contents() function
Example:
<?php
// define string to write
$data = "All the world's a stage\r\nAnd all the men and women
merely players";
// write string to file
file_put_contents(‘file11.txt', $data) or die("Cannot write to file");
echo "File successfully written.";
?>
Note:
The file_put_contents() function provides an easy way to write
data to a file.
The file will be created if it does not already exist, and
overwritten if it does. The
return value of the function is the number of bytes written.
File Sequence Handling
1. Always start by opening the file. This is done through a call to fopen.
2. Then you can call other functions; here we write to the file (fwrite), but you
can also read from an existing file (fread or fgets) and do other things.
3. Finish by closing the file (fclose). Although the program does this for you
when it ends, you should clean up yourself by closing the file when you’re
finished.
Different types of parameters mode
Mode Action Description
'r' Read from file start Open for reading only; place the file pointer at the beginning of the file. Return
FALSE if the file doesn’t already exist.
'r+‘ Read from file start and allow
writing Open for reading and writing; place the file pointer at the beginning of the file.
Return FALSE if the file doesn’t already exist.
'w' Write from file start and truncate file
Open for writing only; place the file pointer at the beginning of the file and
truncate the file to zero length. If the file doesn’t exist, attempt to create it.
'w+' Write from file start, truncate
file and allow reading 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 doesn’t exist, attempt to create it.
'a‘ Append to file end Open for writing only; place the file pointer at the end of the file. If
the file doesn’t exist, attempt to create it.
'a+' Append to file end and allow
reading Open for reading and writing; place the file pointer at
the end of the file. If the file doesn’t exist, attempt to
create it.
Reading from Files
The easiest way to read from a text file is to grab a whole line through fgets
(think of the final s
as standing for “string”)
Example: Reading a file with fgets
<?php
$fh = fopen("testfile.txt", 'r') or
die("File does not exist or you lack permission to open it");
$line = fgets($fh);
fclose($fh);
echo $line;
?>
Moving a File
To move a file, rename it with the rename function.
Example:
<?php // movefile.php
if (!rename('testfile2.txt', 'testfile2.new'))
echo "Could not rename file";
else echo "File successfully renamed to 'testfile2.new'";
?>
Note:You can use the rename function on directories, too. To avoid any
warning messages,
if the original file doesn’t exist, you can call the file_exists function first to
check.
Deleting a File