Page 1 of 1

SQL and filesystem array comparison

Posted: Sat Apr 11, 2009 2:27 pm
by RedAphex
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++;
               }
           }   
        }
 

Re: SQL and filesystem array comparison

Posted: Sat Apr 11, 2009 3:06 pm
by yacahuma
if your are syncing the whole time with the file system, why do you need to put it in a database? Why dont read the filesystem directly?

Re: SQL and filesystem array comparison

Posted: Sat Apr 11, 2009 4:57 pm
by RedAphex
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.

Re: SQL and filesystem array comparison

Posted: Sat Apr 11, 2009 5:10 pm
by RedAphex
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?

Code: Select all

 
     if( ($catIs_inDB AND !$isIn_FL) ){ //FOLDER in database doesn't exist on server = REMOVE ENTRY
                    echo "TEST";
                    $setForUpdate['remFolders'][$fld_rem] = $currentObject;
                    $fld_rem++;
     } 
     if( ($isIn_DB AND !$isIn_FL ) ){ //FILE in database doesn't exist on server = REMOVE ENTRY
                    echo "TEST";
                    $setForUpdate['remFiles'][$fil_rem] = $currentFilename;
                    $fil_rem++;
     }
 

Re: SQL and filesystem array comparison

Posted: Sat Apr 11, 2009 8:16 pm
by yacahuma
how often are you planning to sync the files? If you dont sync constantly, you could be showing an old file structure.

Re: SQL and filesystem array comparison

Posted: Sat Apr 11, 2009 10:34 pm
by RedAphex
The function is currently set to run each page refresh so I can observe the results.

Re: SQL and filesystem array comparison

Posted: Sun Apr 12, 2009 7:15 am
by yacahuma
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

Re: SQL and filesystem array comparison

Posted: Sun Apr 12, 2009 7:26 am
by RedAphex
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.