Question

I am using a mongo database and and PHP to compare file meta information to a directory of files to see whats missing where. We then return an associative array of files and database to see whats missing and from where.

Note: You do not need to know mongo in order to help me out.

For example, if I delete a file from the directory, yet there is an entry in the database my array will look like such:

array(2) {
  'File System' =>
  array(1) {
    [0] =>
    string(28) "50e72bce045aca4c04000004.txt"
  }
  'Database' =>
  array(0) {
  }
}

The code for this check is done bellow:

public function compareDatabaseToDirectory(){
    // Fetch all file meta information
    $files = $this->_model->fetchAll(array());

    // Create a file based on id and extension.
    foreach($files as $id=>$model){
       $dataBaseEntries[] = $id . $model->fileExtension;
    }

    // Get all files in our directory
    $directoryFiles = preg_grep('/^([^.])/', scandir($this->getDestination()));

    // Get the differneces of files
    $differences = array_diff($dataBaseEntries, $directoryFiles);

    // Check if missing from database OR missing from file system
    $missingFromDataBase = array_diff($differences, $dataBaseEntries);
    $missingFromFileSystem = array_diff($differences, $directoryFiles);

    // Create an associative array
    $filesMissing = array(
          'File System' => $missingFromFileSystem,
          'Database' => $missingFromDataBase
   );

   // Return results.
   return $filesMissing;
}

All were doing here is using a mongo id and appending it to a file extension to create said file.

we compare the array of files against that in the directory of files and return the differences.

In my test all we do is:

public function testComapreDatabaseToFileSystemMissingFile(){
    $this->resetNoSqlDatabase();

    unlink(APPLICATION_PATH . '/data/uploadedFiles/testing/50e72bce045aca4c04000004.txt');
    $result = $this->_file->compareDatabaseToDirectory();

    $expectedArray = array(
        'File System' => array(
            '50e72bce045aca4c04000004.txt'  
        ),
        'Database' => array(),                      
    );


    $this->assertEquals($result, $expectedArray);

}

Which returns me the array you see above. How ever if I replicate the same concept in the browser my array you see above is empty when it should not be.

Any ideas?

Was it helpful?

Solution

It was a folder permissions issue. We created the folder before setting the folder to 0777.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top