function name(s) | category |
---|---|
file, file_get_contents, file_put_contents | reading/writing entire files |
basename, file_exists, filesize, fileperms, filemtime, is_dir, is_readable, is_writable, disk_free_space | asking for information |
copy, rename, unlink, chmod, chgrp, chown, mkdir, rmdir | manipulating files and directories |
glob, scandir | reading directories |
contents of foo.txt | file("foo.txt") | file_get_contents("foo.txt") |
---|---|---|
Hello how r u? I'm fine |
array("Hello\n", #0 "how r u?\n", #1 "\n", #2 "I'm fine\n" #3 ) |
"Hello\n how r u\n # a single \n # string I'm fine\n" |
The file
function returns lines of a file as an array (\n at end of each).
file_get_contents
returns entire contents of a file as a single string.
file_put_contents
writes a string into a file.
file
Function
# display lines of file as a bulleted list
$lines = file("todolist.txt");
foreach ($lines as $line) { # for ($i = 0; $i < count($lines); $i++)
print $line;
}
file
returns the lines of a file as an array of strings.
Each ends with \n; to strip it, use an optional second parameter:
$lines = file("todolist.txt", FILE_IGNORE_NEW_LINES);
Common idiom: foreach or for loop over lines of file
$array = explode(delimiter, string);
$string = implode(delimiter, array);
$s = "CSE 154 A";
$a = explode(" ", $s); # ("CSE", "154", "A")
$s2 = implode("...", $a); # "CSE...154...A"
explode
and implode
convert between strings and arrays.
For more complex strings, you can use regular expressions.
explode
Marty D Stepp
Jessica K Miller
Victoria R Kirst
foreach (file("names.txt") as $name) {
$tokens = explode(" ", $name);
print "author: {$tokens[2]}, {$tokens[1]}\n";
}
author: Stepp, Marty
author: Miller, Jessica
author: Kirst, Victoria
list
list($var1, ..., $varN) = array;
Kyle Thayer
(206) 154 2017
17-154-0123
list($name, $phone, $ssn) = file("personal.txt");
...
list($area_code, $prefix, $suffix) = explode(" ", $phone);
The list function "unpacks" an array into a set of variables.
When you now a file or line's exact length/format, use file and list to unpack it
function | description |
---|---|
glob | returns an array of all file names that match a given pattern (returns a file path and name, such as "foo/bar/myfile.txt") |
scandir | returns an array of all file names in a given directory (returns just the file names, such as "myfile.txt") |
Can accept a general path with the * wildcard (more powerful).
glob
Example
# reverse all poems in the poetry directory
$poems = glob("poetry/poem*.dat");
foreach ($poems as $poemfile) {
$text = file_get_contents($poemfile);
file_put_contents($poemfile, strrev($text));
print "I just reversed " . basename($poemfile) . "\n";
}
glob can match a wildcard path with the * character
glob("foo/bar/*.doc")
returns all .doc files in the foo/bar
subdirectoryglob("food*")
returns all files whose names begin with "food"The basename
function strips any leading directory from a file path
basename("foo/bar/baz.txt")
returns "baz.txt"scandir
Example
foreach (scandir("taxes/old") as $filename) {
print "I found a file: {$filename}\n";
}
.
..
2007_w2.pdf
2006_1099.doc
scandir
includes current directory (".") and parent ("..") in the array.
Don't need basename
with scandir
; returns file names only
without directory
# reverse a file
$text = file_get_contents("poem.txt");
$text = strrev($text);
file_put_contents("poem.txt", $text);
file_get_contents
returns entire contents of a file as a string
file_put_contents
writes a string into a file, replacing its old
contents
# add a new line to a file
$new_text = "P.S. ILY, GTG TTYL!~";
file_put_contents("poem.txt", $new_text, FILE_APPEND);
old contents | new contents |
---|---|
Roses are red
Violets are blue All my base Are belong to you. |
Roses are red
Violets are blue All my base Are belong to you. P.S. ILY, GTG TTYL!~ |
file_put_contents
can be called with an optional third parameter to append
(add to
end) rather than overwrite.