CS174
Chris Pollett
Feb 29, 2016
$fileHandle = fopen("my.dat", "r"); $file_string = fread($fileHandle, filesize("my.dat")); fclose($fileHandle);Here fread reads in its second parameter many bytes.
$line = fgets($fileHandle, $max_num_bytes_line);
$string = file_get_contents("my.dat");
$lines = file("my.dat");
$fileHandle = fopen("my.dat", "w"); // use "a" for append fwrite($fileHandle, $out_data); fclose($fileHandle);
file_put_contents("out.dat", $str);
$fp = fopen("/tmp/lock.txt", "w"); if (flock($fp, LOCK_EX)) { // do an exclusive/write lock. use LOCK_SH (for shared/read lock) fwrite($fp, "Write something here\n"); flock($fp, LOCK_UN); // release the lock } else { echo "Couldn't lock the file !"; } fclose($fp);Locks are released when fclose() is called.
rename($oldname, $newname); //like Unix mv command, renames/moves a file copy($source, $dest); //copies a file, like Unix cp unlink($filename); // delete a file mkdir($path); // make a directory rmdir($path); // delete a directory link($target, $name); // create a link chmod($filename, $mode); // change file permissions chown($filename, $user); // change owner of a file or dir or link chgrp($filename, $group); //change group of a file or dir or link stat($filename); //returns an array saying size, last access, last modified, etc. for a file touch($filename [, int $time = time() [, int $atime ]]); //sets modification/ access times of a file
if(file_exists('/path/to/folder')) { //file_exists check if a file or dir exists $h = opendir('/path/to/folder'); //can use is_dir to check is something is a directory while (($item = readdir($h)) !== false) { $type = is_dir($item) ? "directory" : (is_link($item) ? "link" : "file"); //there is also a is_file echo "$item is a $type\n"; } }
$text_files = glob("/some_path/*.txt"); foreach($text_files as $file_name) { //do something }
Which of the following statements is true?
$_SESSION["test"]=37; /* sets the test session variable*/ echo $_SESSION["test"];
('Toyota', 'Corolla', '1970-01-01')