Tuesday, July 20, 2010

File System Functions Quick Reference

basename() Returns the filename component of a path

copy() Copies a file

delete() See unlink() or unset()

fclose() Closes an open file

feof() Tests for end-of-file on an open file

fgetcsv() Parses a line from an open file, checking for CSV fields

fgets() Returns a line from an open file

file() Reads a file into an array

file_exists() Checks whether or not a file or directory exists

file_get_contents() Reads a file into a string

file_put_contents Writes a string to a file

filesize() Returns the file size

fopen() Opens a file or URL

fputs() Alias of fwrite()

unlink() Deletes a file

fread() Reads from an open file


Example: Read csv file:

Ref: http://php.net/manual/en/function.fgetcsv.php

$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "

$num fields in line $row:

\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "
\n";
}
}
fclose($handle);
}

No comments:

Post a Comment