I have some problem to delete file when some value deleted.
This is my delete function:
public function delete($id, Adopsi $adopsi)
{
Storage::delete('post/adopsi/' . $adopsi->image_post_adps);
$data = Adopsi::where('id', $id)->delete();
if ($data) {
return redirect()->route('adopsi.index')->with('success', 'Data telah dihapus');
} else {
return redirect()->route('adopsi.index')->with('error', 'Data gagal dihapus');
}
}
This is how I store my image before:
$imageSize = $request->file('image_post_adps')->getSize();
$imageName = $request->file('image_post_adps')->getClientOriginalName();
$request->file('image_post_adps')->storeAs('public/post/adopsi', $imageName);
directory folder where is use to save:
storage/app/public/post/adopsi/..... (there's my file),
I've used Storage::delete() and unlink() but it still isn't working.
Your images are stored at 'public' disk path. But disk is not specified in delete()
Try
Storage::disk('public')->delete('post/adopsi/' . $adopsi->image_post_adps);
First, make sure the file exists by building the path:
First of all, add the File Facade at the top of the controller:
use Illuminate\Support\Facades\File;
You can use public_path() & storage_path():
$path = public_path('../storage/YOUR_FOLDER_NAME/YOUR_FILE_NAME');
if (!File::exists($path)) {
File::delete(public_path('storage/YOUR_FOLDER_NAME/YOUR_FILE_NAME'));
}
$path = storage_path().'/app/public/post/adopsi/'.$db->image;
if(File::exists($path)) {
File::delete($path);
}
The below worked for me
$destinationPath = storage_path() . '/app/public/images/products';
File::delete($destinationPath . '/' . $product->id . '.' . $fileExtension);
Related
I am trying to delete an image that is saved in storage/app/public after one day.
I followed this answer but cannot manage to make it work. This is my first time doing anything on kernel.php so appreciate it if someone can help me figure out what Im doing wrong or if theres an easier way to do it.
Controller
// set path
$pathMedia = "/media" . "/" . $aid . '-' . uniqid() . ".jpg";
// store received image
Storage::disk("public")->put($pathMedia, $image);
// insert new received media
$media = new ReceivedMedia();
$media->media = $pathMedia;
$media->type = 'image';
$media->delete_at = Carbon::now()->addDays(1)->format('Y-m-d');
$media->save();
Kernel.php
use Carbon\Carbon;
$schedule->call(function () {
$files = DB::table('received_media')->whereDate('delete_at', Carbon::now()->format('Y-m-d'))->get();
foreach ($files as $file) {
$mediaPath = public_path('storage') . $file->media;
if ($file->media != null && File::exists($mediaPath)) {
unlink($mediaPath);
}
$file->delete();
}
})->daily();
You are storing the file in Storage and trying to delete it from the public folder.
You should delete it from the storage instead of public
if (Storage::disk('public')->exists($file->media)) {
Storage::disk('public')->delete($file->media);
}
I upload from vue 4-5 images to different folders in laravel.
public function uploadImageso2orders(Request $request, $id)
{
$o2order = O2order::findOrFail($id);
$name = $o2order->contactname;
$name2 = str_replace(' ', '_', $name);
$image = $request->file('file');
$imageName = $name2.'.'.time().'.'.$image->extension();
$wwwPath = 'https://api2.api.sk/storage/';
$image->move(storage_path('app/o2/servisne' . '/' . $name2),$imageName);
$imagePath = 'https://api2.api.sk/storage/app/o2/' . ('servisne' . '/' . $name2);
$o2order->servisny = $imagePath;
$o2order->save();
}
This is working fine. In storage/app/o2/servisne_listy/ is folder created and multiple images stored. In sql is written full path to this folder.
I need to show this image or images, sometimes its one sometimes more, not same.
I have this:
public function showImageso2orders(Request $request, $id)
{
$o2order = O2order::findOrFail($id);
$servisny = $o2order->servisny;
return response()->json(['servisny' => $servisny], 200);
}
Its shows only the path from sql, but i need path to every file which is in this directory.
Thanks
May be this help you
$path = storage_path('app/o2/servisne');
$files = File::files($path)
I'm trying to remove a folder with images in it.
here is my method for removing the file
public function deleteCar($id) {
$car = Car::find($id);
$carImages = carImage::where('car_id', $id);
foreach ($carImages as $image) {
//Just for testing purposes.
$image->car_image_path = '/uploads/cars/32/exampleImage.png';
$pathWords = explode('/', $image->car_image_path);
$path = $pathWords[0] . '/' . $pathWords[1] . '/' . $pathWords[2] . '/' . $pathWords[3];
File::deleteDirectory($path);
$image->delete();
}
$car->delete();
return response()->json(['error' => false, 'data' => $id]);
}
What I want to accomplish is to remove the folder and then the model with the correct car_id. That is passed.
The folder does not get removed and neither does the image model.
You have to use public_path to delete directory:
File::deleteDirectory(public_path('uploads/cars/32'));
The method will return true if it succeeds, false if it fails.
Hope it helps
I'm saving files locally in Laravel, however I'm having issues getting the right URL and accessing the files.
I've setup a symlink with Artisan:
php artisan storage:link
When saving, I add public/ to the name, so the files are placed in the /storage/app/public/ directory, which works.
if ($request->hasFile('files')) {
$files = array();
foreach ($request->file('files') as $file) {
if ($file->isValid()) {
$name = time() . str_random(5) . '.' . $file->getClientOriginalExtension();
Storage::disk('public')->put($name, $file);
$files[] = $name;
}
}
if (count($files) > 0) {
$response->assets = json_encode($files);
}
}
The name is stored in the database
["1524042807kdvws.pdf"]
Then the assets are returned as part of a JSON object via my API for Vue
if (count($response->assets) > 0) {
$assets = array();
foreach (json_decode($response->assets, true) as $asset) {
$assets[] = asset($asset);
}
$responses[$key]->assets = $assets;
}
Which returns http://127.0.0.1:8000/1524042807kdvws.pdf but that 404s. I've gotten myself a little confused I think, so any pointers or help would be appreciated.
So I found my answer on another post
I needed to wrap the $file in file_get_contents();
Storage::disk('public')->put($name, $file);
and instead of asset() I used:
Storage::disk('public')->url($asset['file']);
Check this docs. https://laravel.com/docs/5.5/filesystem#the-public-disk
As it shows there instead of
$name = 'public/' . time() . str_random(5) . '.' . $file->getClientOriginalExtension();
you can just have
$name = 'time() . str_random(5) . '.' . $file->getClientOriginalExtension();
Storage::disk("public")->put($name, $file); // assuming you have the public disk that comes by default
Then when you want to get an url, you can use the asset function
asset('storage/foo.txt');
i am trying to upload a photo using Image Intervention Package
i tried dd($request->hasFile('avator') ) and it return false
i returned "error" in my if statment just to make sure that i have an error
thanks in advance
public function update_photo(Request $request){
if($request->hasFile('avator') ){
$avator = $request->file('avator');
$filename = time() . '.' . $pic->getClientOriginalExtension();
Image::make($avator)->resize(300 , 300)->save(public_path('/uploads/avators' . $filename));
$user = Auth::user();
$user->avator = $filename ;
$user->save();
echo "hello world !";
}else {
echo " Error"; // just to make sure that i have an error
}
return view("profile" , array("user" => Auth::user() ));
}
first edit this line of code
$filename = time() . '.' . $avator->getClientOriginalExtension();
add to this slash also
Image::make($avator)->resize(300 , 300)->save(public_path('/uploads/avators/' . $filename));
*in your blade template make sure of spelling you are writing avator not avatar *
<input type="file" name="avator"/>
In your blade put this in your form tag:
enctype="multipart/form-data
and in your controller:
use File;