PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
$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++;
}
}
}
Last edited by RedAphex on Sat Apr 11, 2009 10:36 pm, edited 1 time in total.
Its meant to be a part of an update function for the site's administrator. I'm concerned that the volume of files he wants to place would cost significantly more resources to sync for each page view rather than just when the information needs to be updated.
I fixed the folder-add section so both conditionals for adding things are working correctly now. I have a sneaking suspicion that there is something syntactically wrong with the conditionals as the test echos don't fire - and they should due to the test entries I made. Can anyone verify this?
if you are syncing in every refresh page, then you are actually taking longer than just looking at the file system. There is no need for you to save anything on the database. Think about what you are saying.
Option1: on every refresh page , read the directory, open database connection, check the difference in the database and update the database accordingly, then display results
Option 2: read directory, then display results
I understand what you're saying but the reason it runs the function on each page refresh is a temporary tweak so that I can see the results before putting the function into action. I intend to have it run when a button is clicked on the administrative page only when I'm content with the results in the test.
I'm still unsure as to why the remove files conditionals don't work. There are no errors and, as you can see, the add conditionals are in the same loop and are working just fine. I'll probably just write a work around for now but it would be great if someone knew why its broken.