Fixed image folder deletion. Fixes #22.

This commit is contained in:
Dan Brown 2015-08-23 14:05:09 +01:00
parent 958ed627dd
commit 40b629d35d
2 changed files with 23 additions and 6 deletions

View File

@ -177,14 +177,30 @@ class ImageController extends Controller
// Delete files // Delete files
$folder = public_path() . dirname($image->url); $folder = public_path() . dirname($image->url);
$pattern = '/' . preg_quote(basename($image->url)). '/'; $fileName = basename($image->url);
$dir = new RecursiveDirectoryIterator($folder);
$ite = new RecursiveIteratorIterator($dir); // Delete thumbnails
$files = new RegexIterator($ite, $pattern, RegexIterator::ALL_MATCHES); foreach(glob($folder . '/*') as $file) {
foreach($files as $path => $file) { if(is_dir($file)) {
unlink($path); $thumbName = $file . '/' . $fileName;
if(file_exists($file)) {
unlink($thumbName);
}
// Remove thumb folder if empty
if(count(glob($file . '/*')) === 0) {
rmdir($file);
}
}
} }
// Delete file and database entry
unlink($folder . '/' . $fileName);
$image->delete(); $image->delete();
// Delete parent folder if empty
if(count(glob($folder . '/*')) === 0) {
rmdir($folder);
}
return response()->json('Image Deleted'); return response()->json('Image Deleted');
} }

View File

@ -70,6 +70,7 @@ Route::group(['middleware' => 'auth'], function() {
Route::get('/images/all', 'ImageController@getAll'); Route::get('/images/all', 'ImageController@getAll');
Route::put('/images/update/{imageId}', 'ImageController@update'); Route::put('/images/update/{imageId}', 'ImageController@update');
Route::delete('/images/{imageId}', 'ImageController@destroy'); Route::delete('/images/{imageId}', 'ImageController@destroy');
Route::get('/images/{imageId}/delete', 'ImageController@destroy');
Route::get('/images/all/{page}', 'ImageController@getAll'); Route::get('/images/all/{page}', 'ImageController@getAll');
Route::get('/images/{any}', 'ImageController@getImage')->where('any', '.*'); Route::get('/images/{any}', 'ImageController@getImage')->where('any', '.*');