laravel 如何在laravel 4中删除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16951734/
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-09 02:28:45  来源:igfitidea点击:

How to delete a file in laravel 4

laravellaravel-4

提问by Chachoo Aguirre

I'm using laravel 4 and I need to change an uploaded image, I have it in:

我正在使用 laravel 4,我需要更改上传的图片,我在:

Public
--uploads
---news
----id_news.jpg

When editing the new's I want to make a change image form, but how could I delete and upload another file. I am using:

编辑新文件时,我想更改图像表单,但如何删除并上传另一个文件。我在用:

Input::file('img')->move('uploads/news', $id.'_news.jpg');

The problem it's that it doesn't work, it's not replacing the file, so how could I delete The image so I could upload again.

问题是它不起作用,它没有替换文件,所以我怎么能删除图像以便我可以再次上传。

In laravel 3 I only used:

在laravel 3中,我只使用了:

File::delete('path/to/file');

But I don't see anything about removing files in laravel docs.

但是我没有看到有关在 laravel 文档中删除文件的任何内容。

采纳答案by DaGardner

There is still the delete method in Laravel 4:
src/Illuminate/Filesystem/Filesystem.php

Laravel 4中还有delete方法:
src/Illuminate/Filesystem/Filesystem.php

otherwise just use good old unlink()!?

否则就用好旧unlink()!?

回答by Abbas Palash

I think you should append public_path() to file names , to get real file path, like this

我认为你应该将 public_path() 附加到文件名,以获得真实的文件路径,就像这样

File::delete(public_path().$id.'_news.jpg');

回答by Waiyl Karim

You can easily do something like:

您可以轻松地执行以下操作:

$filename = public_path().'/uploads/foo.bar';

if (File::exists($filename)) {
    File::delete($filename);
} 

Reference: Laravel-recipes Delete a File

参考:Laravel-recipes 删除文件

回答by Adrian Enriquez

Other delete usage:

其他删除用法:

// 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);

Source: http://laravel-recipes.com/recipes/133/deleting-a-file

来源:http://laravel-recipes.com/recipes/133/deleting-a-file

回答by im-sunil

$destinationPath = 'uploads/my-image.jpeg'; // from public/

if ( File::exists($destinationPath) ) {
  File::delete($destinationPath);
}

回答by Jawad

This works on laravel 4.2.

这适用于 Laravel 4.2。

File::delete(storage_path()."/ProductSalesReport-20150316.csv");

// Here are some other paths to directories laravel offers, Hope this
   helps

/* Path to the 'app' folder */
echo app_path();

/* Path to the project's root folder */
echo base_path();

/* Path to the 'public' folder */
echo public_path();

/* Path to the 'app/storage' folder */
echo storage_path();