"; // filesize takes a filename, not file instance
// Once read, the pointer points at the end of file
// If we try to read again, nothing is returned from fread()
// echo "read a byte ---> " . fread($filestream, 1) . "
";
// 3. Close the file
// Upon completion of any file operation,
// the filestream must be destroyed by specifying it as an argument
// to the fclose() function.
fclose($filestream);
echo "
";
// a shorter way to open, read entire file, close file
// file_get_contents("filename");
$content = file_get_contents($fname);
echo $content ."
";
// on the other hand, we can open, write entire content, close file
// file_put_contents("filename", "content-to-be-written");
file_put_contents("data/copy-data.txt", $content);
// chmod(string $filename, int $mode) returns true if success, false otherwise
// mode: 1=exe permission; 2=write permission; 4=read permission
// eg, 755 gives all permission to owner, read and exe to everyone else
// fileperms(filename) returns type of file and permissions based on OS
// To display as octal value (similar to when we do ls -l using command line),
// we need to format it, use sprintf and specify format %o
// sprintf(string_format, value_to_format) returns string produced according to the formatting string
// %o treat the argument as an integer and present it as an octal number
// Octal number system: a number system that display all numbers only from 0 to 7.
// It groups binary numbers into 3 digits (e.g., 000, 001, 010, 011, 100, 101, 110, 111)
// use substr(string_to_be_cut, offset)
// offset allows us to cut from end of the string
echo "before chmod --> " . sprintf('%o', fileperms("data/copy-data.txt")) . "
"; // show 6 digits
// echo "permission on data/copy-data.txt is " .
// substr(sprintf('%o', fileperms("data/copy-data.txt")), 2) . "
"; // cut from front
if (chmod("data/copy-data.txt", 0600))
echo "permission on data/copy-data.txt is " .
substr(sprintf('%o', fileperms("data/copy-data.txt")), -4) . "
"; // cut from end
else
echo "fail chmod
";
file_put_contents("data/copy-data.txt", "write file after chmod");
echo "What's in data/copy-data.txt now? --- " . file_get_contents("data/copy-data.txt") . "
";
// notice that, this program creates the file and thus it's the owner.
// Current permission is 0600. The program can write to the file, but noone else can.
// let's see if this program can write to upsorn-file.txt (upsorn is owner, permission 0644)
file_put_contents("upsorn-file.txt", "write content from php program");
// notice that the file only other to read but not write
echo "
";
// check if file exists, get content; otherwise, create one
$filename = "data/test.txt";
$test = ''; // instance of file
if (file_exists($filename))
$test = file_get_contents($filename);
else
file_put_contents($filename, '');
?>
";
}
// EOF is not a character.
// It is a condition provided by the kernel that can be detected by an application.
// The kernel is a computer program at the core of a computer's operating system that has complete control over everything in the system.
// read entire file
$content = file_get_contents("data/sample-data.txt");
echo $content . "
";
echo "
";
?>
";
echo "
";
// write content to a file (write mode)
file_put_contents("data/output.txt", $content);
// difference between "a" and "w"
// open a file in a write mode
$outfile = fopen("data/output-write.txt", "w");
fputs($outfile, $content); // content must be string
fclose($outfile);
// open a file in an append mode
$outfile = fopen("data/output-append.txt", "a");
fputs($outfile, $content); // content must be string
fclose($outfile);
// write a line to file -- this example has logic problem, discuss where the cursor is
$infile = fopen("data/sample-data.txt", "r");
while (!feof($infile))
$line = fgets($infile); // get one line at a time
$outfile = fopen("data/output-write2.txt", "w");
while (!feof($infile))
fputs($outfile, $line); // notice that fputs writes where the file pointer was
fclose($infile);
fclose($outfile); // nothing in output-write2.txt, why? logic problem
// write a line to file
$infile = fopen("data/sample-data.txt", "r");
$outfile = fopen("data/output-write3.txt", "w");
while (!feof($infile))
{
$line = fgets($infile); // get one line at a time
echo "write a line " . $line . "
";
fputs($outfile, $line); // notice that fputs writes where the file pointer was
// since we don't close the file yet, it keeps writing
}
fclose($infile);
fclose($outfile);
// once the file is close, open again puts the pointer at the beginning
// try changing fopen mode to be "a" to see the difference between "a" and "w"
?>
";
echo "
";
// suppose we want to write array data to file
// separate each array item to each line
$days = ["Monday", 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
file_put_contents("data/output-array.txt", $days);
$formatted_days = implode("\n", $days); // note: double quote
file_put_contents("data/output-array.txt", $formatted_days);
$associative_days = Array("Mon"=>"Monday", "Tue"=>"Tuesday", "Wed"=>"Wednesday",
"Thur"=>"Thursday", "Fri"=>"Friday");
// var_dump($days);
var_dump(implode("@", $associative_days)); // implode "@" and value, not key
echo "
";
$content = "";
foreach ($associative_days as $k => $v)
{
$content .= "$k => $v \n";
}
file_put_contents("data/output-array.txt", $content);
?>
";
}
echo "
";
// Write an array to a file
$friends = array("Duh", "Huh", "Wacky");
$content = implode("\n", $friends); // join elements of an array with a \n
echo "write '$content' to file"; // notice that echo doesn't care \n
file_put_contents("data/sample-data3.txt", $content);
echo "
";
?>
File was copied";
}
// Rename a file
$fname2 = "data/datafile2.txt";
$fname_newname = "data/datafile_newname.txt";
if (file_exists($fname2))
{
$rename_success = rename($fname2, $fname_newname);
if ($rename_success)
echo "File was renamed
";
}
// Delete a file
$fname3 = "data/datafile3.txt";
if (file_exists($fname3))
{
$delete_success = unlink($fname3);
if ($delete_success)
echo "File was deleted
";
}
?>