Laravel 8 - Upload storage path changed, how to show old files? - php

In laravel 8, I updated the storage path for image uploads. It was app/violations/filename.jpeg
if( $request->hasfile('violationStatement') )
{
$file = $request->file('violationStatement');
$extension = $file->getClientOriginalExtension();
$filename = $violation->plateNumber . '-' . $violation->violationType . '.' . $extension;
$file->move('app/violations/', $filename);
$violation->violationStatement = $filename;
}
And it was updated to app/violations/speeding/Toyota/XXX-1234/11-June-2022/11-26 AM/filename.jpeg
if( $request->hasfile('violationStatement') )
{
$file = $request->file('violationStatement');
$extension = $file->getClientOriginalExtension();
$filename = 'violation-statement' . '-' . $violation->plateNumber . '-' . $violation->violationType . '.' . $extension;
$file->move('app/violations/' .
$violation->violationType . '/' .
$violation->carModel . '/' .
$violation->plateNumber . '/' .
Carbon::parse($violation->violationDateTime)->format('d-M-Y/H-i A'), $filename);
$violation->violationStatement = $filename;
}
The images are displayed in datatables, so obviously I'll have to change the image src to the new URL. When I do so, how can I also display the old images of the old directory?

Related

Laravel 5.5 + image not readable error (used Intervention\Image)

I used ImageOptimizer package for reducing image size. Source: http://image.intervention.io/getting_started/installation
In controller:
if (Input::hasFile('title_image')) {
$Product = Input::file('title_image');
$filename = time() . '.' . $Product->getClientOriginalExtension();
Image::make($Product)->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename) )->move(public_path() . '/../../products', md5($Product->getClientOriginalName()) . ".png");
$product->title_img = "products/" . md5($Product->getClientOriginalName()) . ".png";
}
How can I fix this error???:
image not readable
Use getRealPath() when you upload an image.
if (Input::hasFile('title_image')) {
$Product = Input::file('title_image');
$filename = time() . '.' . $Product->getClientOriginalExtension();
Image::make($Product->getRealPath())->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename) )->move(public_path() . '/../../products', md5($Product->getClientOriginalName()) . ".png");
$product->title_img = "products/" . md5($Product->getClientOriginalName()) . ".png";
}

Upload file in Laravel - wrong path

I am beginner in Laravel.
I have this code:
if ($request->hasfile('profilePhoto')) {
$this->validate($request, [
'profilePhoto' => 'required',
'profilePhoto.*' => 'mimetypes:image/jpg'
]);
$image = $request->file('profilePhoto');
$extension = strtolower($image->getClientOriginalExtension());
$path = 'upload/images/UserImage/';
$uniqueName = md5($image . time());
$image->move(public_path($path), $uniqueName . '.' . $extension);
}
This function uploads files to public/upload/images/UserImage/.
I need it to store it in storage/app/upload/images/UserImage/ instead
How can I rewrite my code?
You have to use storage_path function ("storage/app/upload" folder must exist):
$image->move(storage_path("app/upload"), $uniqueName . '.' . $extension);
if ($request->hasfile('profilePhoto')) {
$this->validate($request, [
'profilePhoto' => 'required',
'profilePhoto.*' => 'mimetypes:image/jpg'
]);
$image = $request->file('profilePhoto');
$extension = strtolower($image->getClientOriginalExtension());
$path = storage_path('app/public/upload/images/UserImage');
$uniqueName = md5($image . time());
$image->move(public_path($path), $uniqueName . '.' . $extension);
}
A common way in Laravel to upload to storage/app is through the local disk driver.
$file = $path . $uniqueName . '.' . $extension;
\Storage::disk('local')->put($file, $request->file('profilePhoto'));
Storage::disk('local') points to storage/app/.
As your $path variable is already declared like this $path = 'upload/images/UserImage/' .So instead of storing data to public_path you can store to storage_path().
$image->move(storage_path($path), $uniqueName . '.' . $extension);

Laravel can't upload file in storage folder

I used to store my upload files (images) in my public folder but this time I want to store them in my storage folder and I get this error
Can't write image data to path (C:\laragon\www\mynewsite\storage\images/aboutimage-1522481830.png)
Error comes from this line (where I use intervention/image package)
Image::make($image)->resize(1200, 600)->save($location);
My function:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('images/' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}
any idea?
UPDATE
My files will upload in root/storage folder instead of root/storage/app/public/images
why is that?
Update 2
I changed my function to code below and it's uploading where it suppose to upload but it does not delete the old image
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = '/aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}
SOLVED
here is final code:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename); // root storage path
Image::make($image)->resize(1200, 600)->save($location);
Storage::delete('images/' . $indexabout->image); //public storage path
$indexabout->image = $filename;
}
Basically I gave Root/Storage path to store data and Public/Storage path for deleting them in update method.

How do I upload files on 2 folders at once in one method on the laravel?

I use laravel 5.6
I want to upload files on 2 folders at once. So if the image uploaded successfully in the product folder, I want the image uploaded in the thumbs folder as well
I try like this :
public function uploadImage($file)
{
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPath = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product';
if(!File::exists($destinationPath)) {
File::makeDirectory($destinationPath, 0755, true);
}
$file->move($destinationPath, $fileName);
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
If the code run, it just success to upload in the product folder. It did not upload in the thumbs folder
There exist error like this :
message Unable to find file (/tmp/phpUSxbEJ).
exception Intervention\Image\Exception\NotReadableException
I try run this code :
public function uploadImage($file)
{
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
So I remove code to upload in product folder. I try it and it works. It success upload on the thumbs folder
So I think in one process, it only upload in one folder
Is there any other way to upload on 2 folders?
You first upload the temporary file which will be removed once you save it on your disk, thats why u can't reuse it, instead of reusing it, you fetch the saved image and do resizing on it and save it with a different name:
public function uploadImage($file)
{
...
$file->move($destinationPath, $fileName);
//$file here doesn't exist anymore, hence it can't be read
...
$uploadedFile = Storage::get($destinationPath . DIRECTORY_SEPARATOR . $filename);
$image_resize = Image::make($uploadedFile);
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
I find a solution
I try like this :
$file = $file->move($destinationPath, $fileName);
It works

move_uploaded_file and the copy

I am trying to upload a tmp file and then copy it. But it is not appearing in the second folder, is there an overlay or something I have to watch?
$uploadPath = '/.../..../image/';
$uploadPathpreview = '/.../..../blog/';
if ($counter == 0){
$preview_file = $uploadPath . DS . 'preview_' . date("Ymd-Hi-") . $img_name;
$preview_file_save = $uploadPathpreview . DS . 'preview_' . date("Ymd-Hi-") . $img_name;
if(move_uploaded_file($tmp_name, $preview_file)){
$tmp = $preview_file;
$new = $preview_file_save;
}
copy($tmp,$new);
The file in image exists by the way. And I tried it without the if()

Categories