Laravel: Images and Videos are not updating - php

I am using Laravel 5.5, and I have problems to update images and videos.
The thing here is that I have an index of my videos, these videos are being uploades to a folder (\storage\app\public\videos) this path has been linked whith the command:
php artisan storage:link
I have no problem storing any file, I use the following code on my controller:
$videoName = "".$request->user."_".$request->propiedad."_".$videoNumber.".mp4";
$request->video->storeAs('public/videos', $videoName);
As I said everything works fine, but if I try to update this file, I save it with the same name using the same code:
$request->video->storeAs('public/videos', $videoName);
And it saves when i check the folder, but the view displays the old video instead of the new one, i am not sure what is going on I have tried to clear cache but is not working.

Check if the file exist. if yes, then delete it and upload the new one.
$videoName = "".$request->user."_".$request->propiedad."_".$videoNumber.".mp4";
if(Storage::exists('public/videos' . $videoName)) {
Storage::delete('public/videos' . $videoName);
}
// Save the video
$request->video->storeAs('public/videos', $videoName);
Deleting a file
Checking if the file exists

Related

Laravel does not upload video file and store tmp filename in database

I'm working with Laravel 5.8 and I wanted to upload a video file.
So in the Controller, I added this code which does the uploading:
if ($request->file('prd_video')) {
$video = Request::file('prd_video');
$videoname = $video->getClientOriginalName();
$path = public_path().'/upload/video/products/';
$request->file('prd_video')->move($path, $videoname);
$findpro = Product::find($stored->prd_id);
$findpro->prd_video = $videoname;
$findpro->save();
}
Now when I test this, after minutes of loading, the file does not uploaded and store a name like /tmp/phpsMPjG1 as filename in the database.
Also there is no error appearing and the process of insertion seems to be completed successfully.
So what's going wrong here? How can I solve this issue?
edit: previous answer about version of aws-sdk-php was not right.

Laravel cant delete file in remote url using Storage::('s3')->delete($files); but can delete when using aws command

I want to delete these files in my laravel like this. But it doesnt work!!
use Storage;
class SomeController extends Controller
{
public function delete()
{
$filesToDelete = [
'http://minio:9100/minio/myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg',
'http://minio:9100/minio/myapp/1/YFrdE0sarsAcpfFyrifd2.jpeg'
];
// cant delete using this
Storage::disk('s3')->delete($filesToDelete);
}
}
But when I try to delete using this command in my bash it works perfectly
aws --endpoint-url http://minio:9100 s3 rm s3://myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg
delete: s3://myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg
my aws configure list is exactly the same in my project config!
I can use Storage to upload the files but when I try to delete the files it doesnt work:
// Working
Storage::disk('s3')->put('1/ImNkE0wwrstgSpzFyruw8.jpeg', $file);
Am I doing something wrong? Is there anyway to delete these files using laravel??
https://docs.min.io/docs/aws-cli-with-minio
https://readouble.com/laravel/5.4/en/filesystem.html
I think the problem is file path. Try to run as mentioned below.
Storage::disk('s3')->delete('myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg');
I solving this parsing url, like this:
$file_path = parse_url($file_url);
//return $file_path['path'];
Storage::disk('s3')->delete($file_path);
it works for me.
In your bucket, the files has a key, thath be need to delete file from your php function. to get this the object url be parsed.
You can do it by this way
$file_name='your_file_path';
unlink(storage_path($file_name));
You are adding wrong file path.In order to remove image form s3 bucket you are not need to give full path of image, Just add root path of image.
To delete multiple images
$files = [
'myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg',
'myapp/1/YFrdE0sarsAcpfFyrifd2.jpeg'
];
Storage::disk('s3')->delete($files);
Delete single image
$file = 'myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg';
Storage::disk('s3')->delete($file);

Laravel 6 Storage results in a 404 error when trying to fetch files

I have tried to setup an upload script in Laravel and have followed the instructions in the docs.
I created a Symlink using the Laravel script and it looks like the following
storage -> /Users/username/Sites/switch/storage/app/public
The problem arrives when I go to upload the image and then get result of the image url in return. As you can see to match the symlink I set the folder to be public below.
$path = $request->file('manufacturer_image_name')->store('public');
echo asset($path);
and this returns
http://127.0.0.1:8000/public/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png
the problem is this doesn't work and throws a 404 but if I manually change the url from "public" to "storage" it will find the image.
http://127.0.0.1:8000/storage/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png
Shouldn't
echo asset($path);
be returning a url containing storage instead of public?
assett($path) is for generating a URL for assets that are just in the public folder, things like the Mix generated CSS and JS files. If you user Laravel Storage to save the file, you also have to use Laravel storage to generate the file URL.
Storage::url('file.jpg');
Well, there are a lot of ways to do that, pick anyone which fits you best.
// using storage_path helper
storage_path('public/' . $filename);
// you could make a double-check with File::exist() method
$path = storage_path('public/' . $filename);
if (!File::exists($path)) {
abort(404);
}
// using asset helper
asset('storage/your_folder/image.png');
// using url helper
url('storage/your_folder/image.png');
// using Storage facade
Storage::url($photoLink)
Here is the simplest and exact thing for your issue
if(!empty($request->file('manufacturer_image_name'))){
$path = storage_path('public/image/');
$image_path = Storage::disk('public')->put('manufacturer_image_name', $request->file('manufacturer_image_name'));
//Assuming you have a model called Manufacturer and created $manufacturer = new Manufacturer()
$manufacturer->manufacturer_image_name = isset($image_path) ? "storage/".$image_path : "";
}
Thanks for the help, I discovered this answer the fits nearly perfectly what I am after. Laravel: Storage not putting file inside public folder
This was what I ended up with.
if($request->file('manufacturer_image_name')){
$path = Storage::disk('public')->put('logo', $request->file('manufacturer_image_name'));
echo $path;
}
$path now returns "logo/filename.ext" instead of "public/ or storage/" so I can store this directly in the db.

Cannot delete file with 'Storage::delete'

I cannot delete file by using laravel Storage::delete fuction.
My image store code is :
$logoPath = $request->file('logo')->store('images','public');
It store image at
storage/app/public/images/0HHJ5DygYUXuLC5dgu14KJTPvCEaxhISV8tpb4Pq.png
I use Storage::delete('/images/0HHJ5DygYUXuLC5dgu14KJTPvCEaxhISV8tpb4Pq.png'); to delete it. But it not working;
What I have tried is:
Storage::delete(asset('storage/images/0HHJ5DygYUXuLC5dgu14KJTPvCEaxhISV8tpb4Pq.png'));
Storage::delete(storage_path('images/0HHJ5DygYUXuLC5dgu14KJTPvCEaxhISV8tpb4Pq.png'));`
Even I try to delete some picture at public folder. It's not working.
Storage::delete(public_path('images/logo3.png'));
Did I do something wrong?
this is what works for me, hopefully it works for you too:
Storage::delete('public/images/0HHJ5DygYUXuLC5dgu14KJTPvCEaxhISV8tpb4Pq.png');
The delete method accepts a single filename to remove from the disk:
Storage::delete('public/images/filename.png');
This is the php way of deleting a file. unlink() function.
$file = $request->input('filename');
unlink(storage_path('app/public/images/' . $file));

Create Thumbnail with SWFUpload

I am attempting a CMS photo gallery and need to create thumbnails at the end of my upload.php file. I have a function called create_square_image that works fine when run on the 'showphotos.php' page. However I don't want it to be run every time someone views the page. I have tried adding the following to SWFUpload...
// Process the file
/*
At this point we are ready to process the valid file. This sample code shows how to save the file. Other tasks
could be done such as creating an entry in a database or generating a thumbnail.
Depending on your server OS and needs you may need to set the Security Permissions on the file after it has
been saved.
*/
$thumb_save = "uploads/thumbs/thumb_" . $_FILES[$upload_name]["tmp_name"];
create_square_image($_FILES[$upload_name]["tmp_name"],$thumb_save,100);
if (!#move_uploaded_file($_FILES[$upload_name]["tmp_name"], $save_path.$file_name)) {
HandleError("File could not be saved.");
exit(0);
}
exit(0);
The create_square_image function uses this format:
create_square_image ( string $filename , string $destination, size )
I have tried it a few times and it will save the image but not the thumbnail.
Set the permissions on /uploads/thumbs/ to 777. You could also try specifying the absolute path to that location. To see your absolute path use this code: echo getcwd();

Categories