16709

How to Delete Images from Public/Images Folder in laravel 5 (URL Data)

Question:

how to delete images file from public/images folder in laravel 5 ??

i found some example from this site, but i know they are just using the file name in their record table, but i'm using something like URL e.g localhost/project/uploads/filename.jpg on my record table. so if i tried like this :

$image_path = $data->image; // the value is : localhost/project/image/filename.format if(File::exists($image_path)) { File::delete($image_path); }

the file is not deleted

help pls, thanks

Answer1:

If you want to delete image from your server, you have to reference location of file in directory server, means you could not reference by url link to delete it.

Commonly, Laravel 5 file is locate in <em>public</em> folder.

Example: your files are located in <em>public/images</em>

$image_path = "/images/filename.ext"; // Value is not URL but directory file path if(File::exists($image_path)) { File::delete($image_path); }

If I can delete image from server by reference URL then Google is the first target :)

Answer2:

call this function and pass two parameter $filepath = path where your file exist $filename = name of your file

public static function UnlinkImage($filepath,$fileName) { $old_image = $filepath.$fileName; if (file_exists($old_image)) { @unlink($old_image); } }

Recommend

  • Mysql Invalid datetime format: 1292 Incorrect datetime value when creating some dummy data for testi
  • Precompiling uploaded assets
  • laravel-5.4 - error :Creating default object from empty value
  • How to rotate a PDF file using Ruby
  • PHP file_exists() anomaly
  • MVVM: Image Bind Source from FileOpenPicker
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • GAE: Way to get reference to an HttpSession from its ID?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • Zoom in and out of jPanel
  • What is the purpose of TaskExecutor in spring?
  • Firefox Extension - Monitor refresh and change of tab
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Saving Changes After In-App Purchase Has Been Purchased
  • Python urlparse: small issue
  • Syntax for setting draggablecursor property in google maps api
  • FileReader+canvas image loading problem
  • Why is an OPTIONS request sent to the server?
  • Is there any way to access browser form field suggestions from JavaScript?
  • Resize panoramic image to fixed size
  • Volusion's generic SQL folder, functionality
  • Font Awesome Showing Box instead of Icons
  • To display the title for the current loaction in map in iphone
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • embed rChart in Markdown
  • python draw pie shapes with colour filled
  • Is it possible to post an object from jquery to bottle.py?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • Python/Django TangoWithDjango Models and Databases