laravelÔõÑùɾ³ýÎļþ
laravelÊÇÒ»ÖÖÊ¢ÐеÄwebÓ¦ÓóÌÐò¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÐí¶àÀû±ãµÄÒªÁìÀ´´¦ÀíÎļþºÍÎļþ¼Ð²Ù×÷¡£É¾³ýÎļþÊÇÎļþ²Ù×÷ÖеÄÒ»Ïî»ù±¾Ê¹Ãü£¬laravelÌṩÁ˼¸ÖÖÒªÁìÀ´ÊµÏÖÎļþɾ³ý¡£±¾ÎĽ«ÏÈÈÝlaravelÖÐÔõÑùɾ³ýÎļþµÄÒªÁì¡£
Ò»¡¢Ê¹ÓÃFileÀàɾ³ýÎļþ
LaravelµÄFileÀàÌṩÁËÒ»ÖÖ¼òÆÓµÄÒªÁìÀ´É¾³ýÎļþ¡£×·ËæÏÂÃæµÄ°ì·¨À´É¾³ýÎļþ£º
1.ÔÚÄúµÄ¿ØÖÆÆ÷»òÎļþÖУ¬µ¼ÈëFileÀࣺ
use IlluminateSupportFacadesFile;
2.ʹÓÃFileÀàµÄdelete()ÒªÁìÀ´É¾³ýÎļþ£º
File::delete($filePath);
ÔÚÕâÀ$filePathÊÇÄúҪɾ³ýµÄÎļþµÄ·¾¶ºÍÃû³Æ¡£File::delete()ÒªÁ콫ɾ³ýÖ¸¶¨µÄÎļþ¡£
¶þ¡¢Ê¹ÓÃStorageÀàɾ³ýÎļþ
ÁíÒ»¸öɾ³ýÎļþµÄLaravelÒªÁìÊÇʹÓÃStorageÀà¡£StorageÀàÌṩÁËÒ»¸ö±ã½ÝµÄAPIÀ´ÖÎÀíÎļþ¡£×·ËæÏÂÃæµÄ°ì·¨À´É¾³ýÎļþ£º
1.ÔÚÄúµÄ¿ØÖÆÆ÷»òÎļþÖУ¬µ¼ÈëStorageÀࣺ
use IlluminateSupportFacadesStorage;
2.ʹÓÃStorageÀàµÄdelete()ÒªÁìÀ´É¾³ýÎļþ£º
Storage::delete($filePath);
ÔÚÕâÀ$filePathÊÇÄúҪɾ³ýµÄÎļþµÄ·¾¶ºÍÃû³Æ¡£Storage::delete()ÒªÁ콫ɾ³ýÖ¸¶¨µÄÎļþ¡£
Èý¡¢Ê¹ÓÃPHP unlink()º¯Êýɾ³ýÎļþ
»¹¿ÉÒÔʹÓÃPHP unlink()º¯ÊýÀ´É¾³ýÎļþ¡£ÏÂÃæÊÇʵÏÖ¸ÃÒªÁìµÄ°ì·¨£º
1.ÔÚÄúµÄ¿ØÖÆÆ÷»òÎļþÖУ¬Ö¸¶¨ÒªÉ¾³ýÎļþµÄÍêÕû·¾¶ºÍÃû³Æ£º
$filePath = public_path(‘uploads/’ . $fileName);
ÔÚ´ËʾÀýÖУ¬$fileNameÊÇÄúҪɾ³ýµÄÎļþµÄÃû³Æ£¬’uploads/’ÊÇÎļþ¼Ð·¾¶¡£
2.ʹÓÃunlink()º¯ÊýÀ´É¾³ýÎļþ£º
unlink($filePath);
unlink()º¯Êý½«É¾³ýÖ¸¶¨µÄÎļþ¡£
ËÄ¡¢Ôڴ洢ʱʹÓÃObserversɾ³ýÎļþ
ÈôÊÇÄúµÄÏîĿʹÓÃÊÓ²ìÕßÀ´´¦ÀíÄ£×ÓÊÂÎñ£¬ÄÇôÄú¿ÉÒÔʹÓÃObserverÀ´É¾³ýÎļþ¡£ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ«Ê¹ÓÃObserversɾ³ýÉÏ´«µÄÎļþ£¬ÔÚɾ³ýÄ£×ÓʱŲÓøÃObserver¡£
1.½¨ÉèObserverÀࣺ
php artisan make:observer FileObserver –model=File
2.ÔÚÄúµÄObserverÀàÖУ¬½«Îļþɾ³ý´úÂëÌí¼Óµ½deleting()ÒªÁ죺
public function deleting(File $file)
{
Storage::delete($file->path);
µÇ¼ºó¸´ÖÆ
}
ÔÚÕâÀ$fileÊÇÄ£×ÓÃû³Æ¡£É¾³ýÄ£×Óʱ£¬deleting()ÒªÁ콫ŲÓò¢É¾³ýÎļþ¡£
×ܽá
ÔÚLaravelÖУ¬ÓжàÖÖÒªÁì¿ÉÒÔɾ³ýÎļþ¡£Ê¹ÓÃFileÀàºÍStorageÀàµÄÒªÁìÊÇ×î³£¼ûµÄ£¬ÓÉÓÚËüÃÇÌṩ±ã½ÝµÄAPIÀ´ÖÎÀíÎļþ¡£¿ÉÊÇ£¬ÈôÊÇÄúÐèÒª¸üϸÄåµÄ¿ØÖÆ£¬Ê¹ÓÃunlink()º¯Êý¿ÉÒÔ¸üºÃµØÖª×ãÄúµÄÐèÇó¡£¹ØÓÚÉÏ´«ÎļþµÄÇéÐΣ¬Äú¿ÉÒÔʹÓÃÊÓ²ìÕßɾ³ýÎļþ¡£ÎÞÂÛÄúʹÓÃÄÄÖÖÒªÁ죬Laravel¶¼ÌṩÁËÓÅÒìµÄÎļþ´¦ÀíÄÜÁ¦À´×ÊÖúÄúÍê³ÉÎļþɾ³ý¡£
ÒÔÉϾÍÊÇlaravelÔõÑùɾ³ýÎļþµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡