SQL and filesystem array comparison
Posted: Sat Apr 11, 2009 2:27 pm
So I'm fairly new to PHP and I've been working with a MySQL database that stores file information (among other things.) In the block below I'm comparing the current list of files and folders found in a specific folder on the webserver. Files and folders will be written to the database based off of whether they already exist or not. Likewise, I want to do a reverse comparison to remove any database entries that are not in the current file/folder list. Will you kind gurus take a look for me please? I've little doubt that its something silly(it usually is with me.)
- Adding files works just fine - it only wants to add files that don't exist.
- Each folder is stored in the current list as [foldername]=>foldername, where-as the full urls are [int]=>url in the same array.
- The database entries are stored as [filename.ext]=>Folder.
- I added a non-existing file entry to the database for testing purposes. There is no attempt to remove this entry.
Code: Select all
$fld_add = 0;
$fil_add = 0;
$fld_rem = 0;
$fil_rem = 0;
foreach($current_all as $key => $currentObject)
{
$isIn_FL = in_array($currentObject,$current_all);//Object listed on webserver
if( $key == $currentObject ){ // IF IS a FOLDER in the current object list
$catIs_inDB = in_array($currentObject,$images_DB); //CATEGORY listed in MySQL
if( !$catIs_inDB ){ //FOLDER IS NOT in database = ADD ENTRY
$setForUpdate['addFolders'][$fld_add] = $currentObject;
$fld_add++;
}
if( ($catIs_inDB AND !$isIn_FL) ){ //FOLDER in database doesn't exist on server = REMOVE ENTRY
$setForUpdate['remFolders'][$fld_rem] = $currentObject;
$fld_rem++;
}
}else{ // NOT a FOLDER in the current object list
$currentFilename = substr(strrchr($currentObject,'/'), 1 ); //Grab filename from path
$isIn_DB = array_key_exists($currentFilename,$images_DB); //Filename listed in MySQL
if( !$isIn_DB ){ //FILE IS NOT in database = ADD ENTRY
$setForUpdate['addFiles'][$fil_add] = $currentFilename;
$fil_add++;
}
if( ($isIn_DB AND !$isIn_FL ) ){ //FILE in database doesn't exist on server = REMOVE ENTRY
$setForUpdate['remFiles'][$fil_rem] = $currentFilename;
$fil_rem++;
}
}
}