7.4 Keeping Track of Filenames


You want to make sure that two filenames are not pointing to the same file (this can happen because of hard and soft links).

Technique

Use two associative arrays to keep track of the device and inode number of files that you have used:

 <?php function remember_file ($file_name) {     list($dev, $ino) = stat ($file_name);     clearstatcache();     return !($file_info[$dev . $ino]++) ? 1 : 0; } ?> 

Comments

For two files to be the same, they must have the same device and inode number. Knowing this enables us to create a unique ID using the device and inode number, and then check whether any other file has the same device or inode number. One way to implement this function is as follows :

 <?php // $file_list contains a list of file to check // for uniqueness, if they are unique // we pass them off to the imaginary modify() function foreach ($file_list as $file) {     remember_file ($file) and modify ($file); } ?> 

Another use for this might be reporting which files are the same in a list of files. Consider the following:

 <?php foreach ($file_list as $file_name) {     list ($dev, $ino) = stat ($file_name);     if ($file_info[$dev . $ino]++) {         array_push ($similar_files, $file_name);     }     clearstatcache(); } ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net