Created
May 16, 2013 10:01
-
-
Save magickatt/5590689 to your computer and use it in GitHub Desktop.
Concrete5 fixing missing uploaded files single page controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TestFixfilesController extends Controller | |
{ | |
public function on_before_render() | |
{ | |
$originalsDirectory = '/var/www/your-website.com/missing-files/'; | |
$originalFilenames = scandir($originalsDirectory); | |
// Get the file ID's from the database | |
$db = Loader::db(); | |
$result = $db->Execute('SELECT fID FROM Files'); | |
$array = $result->GetArray(); | |
// Put these in an array... (not necessary but cleaner) | |
$fileIds = array(); | |
foreach ($array as $item) { | |
$fileIds[] = (integer) $item['fID']; | |
} | |
// Instantiate all the file objects | |
$files = array(); | |
foreach ($fileIds as $fileId) { | |
$file = File::getbyID($fileId); | |
$files[$fileId] = $file; | |
} | |
// Check which files are missing | |
foreach ($files as $file) { | |
$path = $file->getPath(); | |
if (! is_file($path)) { | |
// Split the path into directory and filename | |
$missingDirectory = dirname($path); | |
$missingFilename = basename($path); | |
// If the missing file is in the directory of original files | |
if (in_array($missingFilename, $originalFilenames)) { | |
// Set the copy source and destination | |
$source = $originalsDirectory . $missingFilename; // Make sure $directory has a trailing slash | |
$destination = $path; | |
/*var_dump($source); | |
var_dump($destination);*/ | |
// Create the nested directories if they do not exist | |
if (! is_dir($missingDirectory)) { | |
mkdir($missingDirectory, 0777, true); // Change permissions as required | |
} | |
// Copy the original file to where the missing file should be | |
$copied = copy($source, $destination); | |
if (! $copied) { | |
throw new Exception('Cannot copy original file to missing file path'); | |
} | |
} | |
} | |
} | |
exit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment