File I/O

advertisement
File Handling with PHP
Open/Close a File
• A file is opened with fopen() as a “stream”
• PHP returns a ‘handle’ to the file
▫ Used to reference the open file
• Each file is opened in a particular mode
• A file is closed with fclose() or when your script
ends
File Open Modes
‘r’
Open for reading only. Start at beginning of file.
‘r+’
Open for reading and writing. Start at beginning of
file.
‘w’
Open for writing only. Remove all previous
content, if file doesn’t exist, create it.
Open for writing, but start at END of current
content (append).
‘a’
‘a+’
Open for reading and writing, start at END
(append) and create file if it doesn’t exist.
File Open/Close Example
<?php
// open and close file to read
$toread = fopen(‘some/file.txt’,’r’);
fclose($toread);
// open and close file to write
$towrite = fopen(‘some/file.txt’,’w’);
fclose($towrite);
?>
Writing Data
• To write data to a file use
▫ fwrite($handle,$data)
EXAMPLE:
$handle = fopen('people.txt', 'a');
fwrite($handle, ‘\nFred:Male');
fclose($handle);
But first…
• Create the file on the
web server
• Set permissions to write
<?php
/* Write to a file
http://ned.highline.edu/~tostrander/215/file_io/write.php */
// open file to write
$file = fopen('poem.txt', 'w');
$text = "Mama said I'd lose my head
if it wasn't fastened on.
Today I guess it wasn't
'cause while playing with my cousin
it fell off and rolled away
and now it's gone.
~ Shel Silverstein";
// write to the file
fwrite($file, $text);
echo "File created successfully.";
// close file
fclose($file);
?>
Reading Data
• There are two main functions to read data:
• fgets($handle)
▫ Reads a line of data at a time
• fread($handle,$bytes)
▫ Reads up to $bytes of data, stops at EOF (end of
file)
▫ Used to read entire file at once
fgets()
$fileName = "poem.txt";
feof($handle)
returns true if we
have reached end
of file
echo "<h3>Method 1: fgets()</h3>";
// open file to read
$file = fopen($fileName, 'r');
// read file contents one line at a time
while (!feof($file)) {
nl2br($string)
echo nl2br(fgets($file));
converts new line
}
characters into
<br /> tags
fclose($file);
Delimiters
echo "<h3>Method 1: fgets() – Delimiters</h3>";
$fileName = "students.txt";
students.txt
$file = fopen($fileName, 'r');
1^Cheryl^McCalop
2^Laura^Lavell
while (!feof($file)) {
3^Thu^Trinh
$line = fgets($file);
$array = explode("^", $line);
$sid = $array[0];
$first = $array[1];
$last = $array[2];
echo "$sid: $first $last<br />";
}
fclose($file);
fread()
$fileName = "poem.txt";
echo "<h3>Method 2: fread()</h3>";
// open file to read
$file = fopen($fileName, 'r');
// read file contents all at once
$contents = fread($file, 1024);
echo nl2br($contents);
fclose($file);
File Open shortcuts
• There are two ‘shortcut’ functions that don’t
require a file to be opened
▫ $lines = file($filename)
 Reads entire file into an array with each line a
separate entry in the array.
▫ $str = file_get_contents($filename)
 Reads entire file into a single string.
file()
$fileName = "poem.txt";
echo "<h3>Method 3: file()</h3>";
// read entire file into an array
$lines = file($fileName);
foreach($lines as $line)
echo nl2br($line);
file_get_contents()
$fileName = "poem.txt";
echo "<h3>Method 4:
file_get_contents()</h3>";
// read entire file into a string
$fileString = file_get_contents($fileName);
echo nl2br($fileString);
Other File Operations
• Delete file
▫ unlink('filename');
• Rename (file or directory)
▫ rename('old name', 'new name');
• Copy file
▫ copy('source', 'destination');
• And many, many more
▫ www.php.net/manual/en/ref.filesystem.php
Dealing With Directories
• Open a directory
▫ $handle = opendir('dirname');
 $handle 'points' to the directory
• Read contents of directory
▫ readdir($handle)
 Returns name of next file in directory
 Files are sorted as on filesystem
• Close a directory
▫ closedir($handle)
 Closes directory 'stream'
Directory Example
$handle = opendir('.');
while ($fileName=readdir($handle)) {
echo "$fileName<br />";
}
closedir($handle);
Other Directory Operations
• Get current directory
▫ getcwd()
• Change Directory
▫ chdir('dirname');
• Create directory
▫ mkdir('dirname');
• Delete directory (MUST be empty)
▫ rmdir('dirname');
• Change directory permissions
▫ chmod('dirname', value);
• And more...
▫ www.php.net/manual/en/ref.dir.php
Try It
Write a script that:
• Creates a subdirectory called “sub” in file IO
• Creates a file in “sub,” and write some text to the
file
• Renames the file
• Renames the directory
• chmod's the file and the directory to 772
Download