laravel 如何从laravel 5.1中的公共文件夹中删除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33842735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:09:45  来源:igfitidea点击:

How to delete file from public folder in laravel 5.1

laravel

提问by Khan Shahrukh

I want to delete a News from database and when I hit the delete button all data from database deleted but the image is remains in upload folder. So, how do I this to work. thanks

我想从数据库中删除新闻,当我点击删除按钮时,数据库中的所有数据都被删除,但图像仍保留在上传文件夹中。那么,我该如何工作。谢谢



This is my function again but does not delete the image from images/news folder of public directory>

这又是我的功能,但不会从公共目录的图像/新闻文件夹中删除图像>

 public function destroy($id) {
    $news = News::findOrFail($id);
    $image_path = app_path("images/news/{$news->photo}");

    if (File::exists($image_path)) {
        //File::delete($image_path);
        unlink($image_path);
    }
    $news->delete();
    return redirect('admin/dashboard')->with('message','??? ??????? ???  ??');
}

回答by Moris.io

You could use PHP's unlink() method just as @Khan suggested.

您可以按照@Khan 的建议使用 PHP 的 unlink() 方法。

But if you want to do it the Laravel way, use the File::delete() method instead.

但是如果你想用 Laravel 的方式来做,请改用 File::delete() 方法。

// Delete a single file

//删除单个文件

File::delete($filename);

// Delete multiple files

// 删除多个文件

File::delete($file1, $file2, $file3);

// Delete an array of files

// 删除一组文件

$files = array($file1, $file2);
File::delete($files);

And don't forget to add at the top:

并且不要忘记在顶部添加:

use Illuminate\Support\Facades\File; 

回答by Khan Shahrukh

Use the unlinkfunction of php, just pass exact path to your file to unlink function :

使用php的unlink功能,只需将文件的确切路径传递给 unlink 功能:

unlink($file_path);

Do not forget to create complete path of your file if it is not stored in the DB. e.g

如果文件未存储在数据库中,请不要忘记创建文件的完整路径。例如

$file_path = app_path().'/images/news/'.$news->photo;

回答by css.cutter

this method worked for me
First, put the line below at the beginning of your controller:

这种方法对我
有用首先,将下面的行放在控制器的开头:

use File;

below namespace in your php file Second:

在您的 php 文件中的命名空间下方第二个:

 $destinationPath = 'your_path';
 File::delete($destinationPath.'/your_file');

$destinationPath --> the folder inside folder public.

$destinationPath --> public 文件夹中的文件夹。

回答by mohammad mira

First, you should go to config/filesystems.phpand set 'root' => public_path()like so:

首先,你应该像这样去config/filesystems.php设置'root' => public_path()

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => public_path(),
],

Then, you can use Storage::delete($filename);

然后,您可以使用 Storage::delete($filename);

回答by Gilberto S. Rocha

Try to use:

尝试使用:

unlink('.'.Storage::url($news->photo));

Look the dotand concatenationbefore the call of facade Storage.

在调用门面存储之前查看连接

回答by Ogbonna Vitalis

Using PHP unlink()function, will have the file deleted

使用PHPunlink()函数,将文件删除

$path = public_path()."/uploads/".$from_db->image_name;
unlink($path);

The above will delete an image returned by $from_db->image_namelocated at public/uploadsfolder

以上将删除$from_db->image_name位于public/uploads文件夹中返回的图像

回答by Md.Mostafijur Rahman

Try it :Laravel 5.5

试试看:Laravel 5.5

public function destroy($id){  
      $data = User::FindOrFail($id);  
      if(file_exists('backend_assets/uploads/userPhoto/'.$data->photo) AND !empty($data->photo)){ 
            unlink('backend_assets/uploads/userPhoto/'.$data->photo);
         } 
            try{

                $data->delete();
                $bug = 0;
            }
            catch(\Exception $e){
                $bug = $e->errorInfo[1];
            } 
            if($bug==0){
                echo "success";
            }else{
                echo 'error';
            }
        }

回答by Rojith Randula Peiris

public function destroy($id) {
    $news = News::findOrFail($id);
    $image_path = app_path("images/news/".$news->photo);

    if(file_exists($image_path)){
        //File::delete($image_path);
        File::delete( $image_path);
    }
    $news->delete();
    return redirect('admin/dashboard')->with('message','??? ??????? ???  ??');
}

回答by Farshad Fahimi

the easiest way for you to delete the image of the news is using the model event like below and the model delete the image if the news deleted

您删除新闻图像的最简单方法是使用如下所示的模型事件,如果新闻删除,模型将删除图像

at first you should import this in top of the model class use Illuminate\Support\Facades\Storageafter that in the model class Newsyou should do this

首先你应该在模型类的顶部导入它,use Illuminate\Support\Facades\Storage然后在模型类中News你应该这样做

public static function boot(){
    parent::boot();

    static::deleting(function ($news) {
          Storage::disk('public')->delete("{$news->image}");
    })
}

or you can delete the image in your controller with this command

或者您可以使用此命令删除控制器中的图像

Storage::disk('public')->delete("images/news/{$news->file_name}");

but you should know that the default disk is public but if you create folder in the public folder and put the image on that you should set the folder name before $news->file_name

但是你应该知道默认磁盘是公共的,但是如果你在公共文件夹中创建文件夹并将图像放在上面你应该在之前设置文件夹名称 $news->file_name

回答by Ermiyas Tsegabu

First make sure the file exist by building the path

首先通过构建路径确保文件存在

if($request->hasFile('image')){
    $path = storage_path('app/public').'/YOUR_FOLDER/'.$db->image;
      if(File::exists($path)){
          unlink($path);
      }