I have a project in Laravel 5.1
And i have product pictures.
I want to delete picture files when i delete product in database.
My image root is
img/product
I have versions of pictures
public function getVersions()
{
return [
'large' => '/large',
'medium' => '/medium',
'small' => '/small',
'thumbnail' => '/thumbnail',
'root' => ''
];
}
My product image root is
$productImageRoot = public_path('img/product/'.$product_id);
I want to learn What is the best practice to delete all pictures in a secure way.
Here is my code for deleting all pictures of product.
/**
* @desc Ürün resimlerini ve resimlerin bulunduğu klasörleri siler.
* @param $product_id
* @return bool
* @throws \Exception
*/
public function deleteAllPictureFiles($product_id)
{
/**
* Resim türlerinin klasör yollarını çekiyoruz.
*/
$folderPaths = $this->getVersions();
$productImageRoot = public_path('img/product/'.$product_id);
$pictures = (new ProductPicture)->getAllPicturesOfProduct($product_id);
try
{
/**
* Resim yolu yazılabilir ise...
*/
foreach($pictures as $picture)
{
/**
* Tüm resimleri siliyoruz.
*/
foreach($folderPaths as $folderPath)
{
unlink($productImageRoot.$folderPath.'/'.$picture->picture);
}
}
/**
* Tüm klasörleri siliyoruz.
*/
foreach($folderPaths as $folderPath)
{
rmdir($productImageRoot.$folderPath);
}
return TRUE;
}
catch(\Exception $e)
{
/**
* Resim yolu yazılamaz ise hata dönderiyoruz.
*/
throw new \Exception(trans('product.is_not_writable'));
}
This code caused an error that paths is not writable.
Now it is the topic that i don't know exactly. I want to learn chmod options for files and folders. And what should i do during deletion ? What is the most secure way to delete files ? I don't want use chmod 777. I know it is not secure.
You can use Laravel to delete the directory much easiser.
File::deleteDirectory('img/product/'.$product_id);
Also you're right, don't use 777. 755 should work. If 755 doesn't work, but 777 is, the group is probably wrong and you could fix it with chown -R youruser:yourgroup yourlaravelfolder
. Most likely your user and group will be the same.
Make sure to make a backup before attempting any of this though.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.