Description: Gives information about a file */ /* test the effects of rename() on stats of file */ $file_path = dirname(__FILE__); require "{$file_path}/file.inc"; /* create temp file */ $fp = fopen("{$file_path}/lstat_stat_variation1.tmp", "w"); // temp file fclose($fp); // renaming a file and check stat echo "*** Testing stat() for files after being renamed ***\n"; $file_path = dirname(__FILE__); $old_filename = "{$file_path}/lstat_stat_variation1.tmp"; $new_filename = "{$file_path}/lstat_stat_variation1a.tmp"; $old_stat = stat($old_filename); clearstatcache(); var_dump(rename($old_filename, $new_filename)); $new_stat = stat($new_filename); // compare the self stat var_dump(compare_self_stat($old_stat)); var_dump(compare_self_stat($new_stat)); // compare the two stats var_dump(compare_stats($old_stat, $old_stat, $all_stat_keys)); // clear the cache clearstatcache(); echo "\n--- Done ---"; ?> <?php $file_path = dirname(__FILE__); unlink("{$file_path}/lstat_stat_variation1a.tmp");
$dirname = "{$file_path}/stat_variation3"; $old_stat = stat($dirname); clearstatcache(); sleep(2); mkdir("{$dirname}/stat_variation3_subdir"); $file_handle = fopen("{$dirname}/stat_variation3a.tmp", "w"); fclose($file_handle); $new_stat = stat($dirname); // compare self stats var_dump(compare_self_stat($old_stat)); var_dump(compare_self_stat($new_stat)); // compare the stats $affected_members = array(9, 'mtime'); clearstatcache(); sleep(2); var_dump(compare_stats($old_stat, $new_stat, $affected_members, "<")); unlink("{$dirname}/stat_variation3a.tmp"); rmdir("{$dirname}/stat_variation3_subdir"); clearstatcache(); // comparing stats after the deletion of subdir and file echo "-- Testing stat() for comparing stats after the deletion of subdir and file --\n"; $new_stat1 = stat($dirname); // compare self stats var_dump(compare_self_stat($new_stat1)); // compare the stats var_dump(compare_stats($new_stat, $new_stat1, $all_stat_keys, "=")); clearstatcache(); echo "\n*** Done ***"; ?>
/* Prototype: array lstat ( string $filename ); Description: Gives information about a file or symbolic link Prototype: array stat ( string $filename ); Description: Gives information about a file */ $file_path = dirname(__FILE__); require "{$file_path}/file.inc"; /* test the effects on stats with creating file/subdir in a dir */ /* create temp file */ mkdir("{$file_path}/lstat_stat_variation8/"); // temp dir // creating and deleting subdir and files in the dir echo "*** Testing stat() on dir after subdir and file is created in it ***\n"; $dirname = "{$file_path}/lstat_stat_variation8"; $old_stat = stat($dirname); clearstatcache(); sleep(2); mkdir("{$dirname}/lstat_stat_variation8_subdir"); $file_handle = fopen("{$dirname}/lstat_stat_variation8a.tmp", "w"); fclose($file_handle); $new_stat = stat($dirname); // compare self stats var_dump(compare_self_stat($old_stat)); var_dump(compare_self_stat($new_stat)); // compare the stats $affected_members = array(3, 9, 10, 'nlink', 'mtime', 'ctime'); clearstatcache(); var_dump(compare_stats($old_stat, $new_stat, $affected_members, "<")); echo "\n--- Done ---";
* Description: Gives information about a file */ /* test the effects of writing to a file on the stats of the file */ $file_path = dirname(__FILE__); require "{$file_path}/file.inc"; $filename = "{$file_path}/stat_variation2.tmp"; $file_handle = fopen($filename, "w"); // temp file fclose($file_handle); echo "*** Testing stat(): writing to a file ***\n"; // writing to an empty file echo "-- Testing stat() on file after data is written in it --\n"; $old_stat = stat($filename); clearstatcache(); sleep(2); $file_handle = fopen($filename, "w"); // temp file fwrite($file_handle, "Hello World"); fclose($file_handle); $new_stat = stat($filename); // compare self stats var_dump(compare_self_stat($old_stat)); var_dump(compare_self_stat($new_stat)); // compare the stats $comp_arr = array(7, 'size'); var_dump(compare_stats($old_stat, $new_stat, $comp_arr, "<")); clearstatcache(); echo "\n*** Done ***"; ?>
/* Prototype: bool copy ( string $source, string $dest ); Description: Makes a copy of the file source to dest. Returns TRUE on success or FALSE on failure. */ /* Test copy(): checking stat of file before and after after copy operation */ $file_path = dirname(__FILE__); require $file_path . "/file.inc"; echo "*** Test copy() function: stat of file before and after copy ***\n"; $src_file_name = $file_path . "/copy_variation18.tmp"; $file_handle = fopen($src_file_name, "w"); fwrite($file_handle, str_repeat("Hello2world...\n", 100)); fclose($file_handle); $dest_file_name = $file_path . "/copy_copy_variation18.tmp"; clearstatcache(); $stat_before_copy = stat($src_file_name); clearstatcache(); echo "Copy operation => "; var_dump(copy($src_file_name, $dest_file_name)); $stat_after_copy = stat($src_file_name); clearstatcache(); // compare all stat fields except access time $stat_keys_to_compare = array("dev", "ino", "mode", "nlink", "uid", "gid", "rdev", "size", "mtime", "ctime", "blksize", "blocks"); echo "Comparing the stats of file before and after copy operation => "; var_dump(compare_stats($stat_before_copy, $stat_after_copy, $stat_keys_to_compare)); echo "*** Done ***\n"; ?> <?php error_reporting(0); unlink(dirname(__FILE__) . "/copy_copy_variation18.tmp"); unlink(dirname(__FILE__) . "/copy_variation18.tmp");
<?php /* Prototype: bool rename ( string $oldname, string $newname [, resource $context] ); Description: Renames a file or directory */ echo "*** Testing rename() on non-existing file ***\n"; $file_path = dirname(__FILE__); require "{$file_path}/file.inc"; $src_name = "{$file_path}/rename_basic.tmp"; $dest_name = "{$file_path}/rename_basic_new.tmp"; // create the file $fp = fopen($src_name, "w"); $old_stat = stat($src_name); fclose($fp); var_dump(rename($src_name, $dest_name)); // expecting true var_dump(file_exists($src_name)); // expecting false var_dump(file_exists($dest_name)); // expecting true $new_stat = stat("{$file_path}/rename_basic_new.tmp"); // checking statistics of old and renamed file - both should be same except ctime $keys_to_compare = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, "dev", "ino", "mode", "nlink", "uid", "gid", "rdev", "size", "atime", "mtime", "blksize", "blocks"); var_dump(compare_stats($old_stat, $new_stat, $keys_to_compare)); ?> ===Done=== <?php unlink(dirname(__FILE__) . "/rename_basic_new.tmp");
fclose($file_handle); // stat of the file created $file_stat = stat($filename); sleep(2); // now new stat of the dir after file is created $new_dir_stat = stat($dirname); clearstatcache(); // stat contains 13 different values stored twice, can be accessed using // numeric and named keys, compare them to see they are same echo "*** Testing stat(): validating the values stored in stat ***\n"; // Initial stat values var_dump(compare_self_stat($file_stat)); //expect true var_dump(compare_self_stat($dir_stat)); //expect true // New stat values taken after creation of file var_dump(compare_self_stat($new_dir_stat)); // expect true // compare the two stat values, initial stat and stat recorded after // creating file, also dump the value of stats echo "*** Testing stat(): comparing stats (recorded before and after file creation) ***\n"; echo "-- comparing difference in dir stats before and after creating file in it --\n"; $affected_elements = array(9, 'mtime'); var_dump(compare_stats($dir_stat, $new_dir_stat, $affected_elements, '!=', true)); // expect true echo "*** Testing stat(): for the return value ***\n"; var_dump(is_array(stat($filename))); echo "\n---Done---"; $file_path = dirname(__FILE__); unlink("{$file_path}/stat_basic/stat_basic.tmp"); rmdir("{$file_path}/stat_basic");
$link_stat = lstat($sym_linkname); sleep(2); // new stat of the file, after a softlink to this file is created $new_file_stat = stat($filename); clearstatcache(); // stat contains 13 different values stored twice, can be accessed using // numeric and named keys, compare them to see they are same echo "*** Testing stat() and lstat() : validating the values stored in stat ***\n"; // Initial stat values var_dump(compare_self_stat($file_stat)); //expect true var_dump(compare_self_stat($dir_stat)); //expect true var_dump(compare_self_stat($link_stat)); // expect true // New stat values taken after creation of file & link var_dump(compare_self_stat($new_file_stat)); //expect true var_dump(compare_self_stat($new_dir_stat)); // expect true // compare the two stat values, initial stat and stat recorded after // creating files and link, also dump the value of stats echo "*** Testing stat() and lstat() : comparing stats (recorded before and after file/link creation) ***\n"; echo "-- comparing difference in dir stats before and after creating file in it --\n"; $affected_elements = array(9, 10, 'mtime', 'ctime'); var_dump(compare_stats($dir_stat, $new_dir_stat, $affected_elements, '!=', true)); // expect true echo "-- comparing difference in file stats before and after creating link to it --\n"; var_dump(compare_stats($file_stat, $new_file_stat, $all_stat_keys, "==", true)); // expect true echo "Done\n";
/* test the effects of is_link() on stats of hard link */ $file_path = dirname(__FILE__); require "{$file_path}/file.inc"; /* create temp file & link */ $filename = "{$file_path}/lstat_stat_variation14.tmp"; $fp = fopen($filename, "w"); // temp file fclose($fp); echo "*** Checking lstat() and stat() on hard link ***\n"; $linkname = "{$file_path}/lstat_stat_variation14_hard.tmp"; //ensure that link doesn't exists @unlink($linkname); // create the link var_dump(link($filename, $linkname)); $file_stat = stat($filename); $link_stat = lstat($linkname); // compare self stats var_dump(compare_self_stat($file_stat)); var_dump(compare_self_stat($link_stat)); // compare the stat var_dump(compare_stats($file_stat, $link_stat, $all_stat_keys)); // clear the stat clearstatcache(); echo "\n--- Done ---"; ?> <?php error_reporting(0); $file_path = dirname(__FILE__); unlink("{$file_path}/lstat_stat_variation14_hard.tmp"); unlink("{$file_path}/lstat_stat_variation14.tmp");