简体   繁体   中英

How to delete a file in laravel 4

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

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:

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

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

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

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

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

otherwise just use good old unlink() !?

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

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

$destinationPath = 'uploads/my-image.jpeg'; // from public/ if ( File::exists($destinationPath) ) { File::delete($destinationPath); }

This works on 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();

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM