By John_rees

2019-04-15 10:52:17 8 Comments

In my edit detail page there is an image field, I want that when i upload new image old photo will remove from images folder and new photo will upload and if i do not upload pic than during submit no error will generate

here is my image upload code

$file = $request->file('file');
$name = time() . $file->getClientOriginalName();
$file->move('uploads/images', $name);
$employee->file = $name;

How can I unlink it?


@behnam shateri 2019-04-15 11:08:33

you can use unlink for removing previous image file if exists by this code:

    if(file_exists(public_path("your image url"))){
        unlink(public_path("your image url"));

and UploadImage() define as:

private function UploadImage($request){
    $file = $request->file('file');
    $name = time() . $file->getClientOriginalName();
    $file->move('uploads/images', $name);
    $employee->file = $name;

@John_rees 2019-04-15 11:32:07

your answer works but if that image already exist in databse than it will unlink, i want that during add employee time the image I upload it will replace that.\

@behnam shateri 2019-04-16 06:47:28

you can try it now, if image exist before unlink it and then upload.

@Rasa Mohamed 2019-04-15 11:05:02

We can so using unlink in php

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

// Delete a single file

use File;

// Delete multiple files

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

// Delete an array of files

$files = array($file1, $file2);

Note: $filename contain full path and image name like ['']

@Martin Bean 2019-04-15 10:58:15

If you’re updating a model, you can delete the old file and upload the new one if one is provided:

if ($request->hasFile('file')) {
    Storage::delete($employee->file); // If $file is path to old image

    $employee->file = $request->file('file')->store('name-of-folder');

@Rouhollah Mazarei 2019-04-15 11:09:51

It's a good practice to check and see if the file exists and then delete it: Storage::exists($employee->file)

@John_rees 2019-04-15 11:29:14

I got this error Non-static method Illuminate\Database\Eloquent\Model::delete() should not be called statically

@Martin Bean 2019-04-15 11:39:50

@John_rees Because you don’t call the delete() method on the model, you call it on the Storage facade:

@Martin Bean 2019-04-15 11:41:23

@RouhollahMazarei Storage::delete() checks if the file exists and just returns false if it doesn’t, so you can safely use it.

Related Questions

Sponsored Content

38 Answered Questions

[SOLVED] Deleting an element from an array in PHP

  • 2008-12-15 20:28:55
  • Ben
  • 2291440 View
  • 2257 Score
  • 38 Answer
  • Tags:   php arrays

28 Answered Questions

[SOLVED] How can I prevent SQL injection in PHP?

34 Answered Questions

[SOLVED] How to vertically align an image inside a div?

38 Answered Questions

[SOLVED] Lazy load of images in ListView

31 Answered Questions

[SOLVED] How do I auto-resize an image to fit a 'div' container?

42 Answered Questions

36 Answered Questions

[SOLVED] How do I check if a string contains a specific word?

11 Answered Questions

[SOLVED] Cannot unlink file in Codeigniter

31 Answered Questions

[SOLVED] How do I get a YouTube video thumbnail from the YouTube API?

1 Answered Questions

In laravel how to move Image file from one folder to another folder

Sponsored Content