My method to upload file in laravel like this :
private function addPhoto(UploadedFile $photo, $fileName)
{
dd(public_path() . DIRECTORY_SEPARATOR . 'img');
$destinationPath = public_path() . DIRECTORY_SEPARATOR . 'img';
$photo->move($destinationPath, $fileName);
}
The result of dd(public_path() . DIRECTORY_SEPARATOR . 'img'); like this :
C:\xampp\htdocs\myshop\public\img
But I want to change it
So, I want to save image uploaded in here :
C:\xampp\htdocs\myshop\storage\temp
How can I save the image in storage folder?
You can use the exact same way, but replace public_path() with storage_path(). This will link to the storage folder for this application.
$destinationPath = storage_path() . DIRECTORY_SEPARATOR . 'temp';
All path helpers can be found in the Laravel documentation.
Use storage_path.
private function addPhoto(UploadedFile $photo, $fileName)
{
$photo->move(storage_path('temp'), $fileName);
}
Related
I use code below:
$image_name = 'image' . time() . '.' . $request->file('image')->getClientOriginalExtension();
$destinationFolder = public_path('images');
$request->file('image')->move($destinationFolder, $image_name);
but sometime its not working, images are not storing. Im using heroku as host.
Try to use Storage facade :
$path = \Storage::putFile('images', $request->file('image'));
Laravel will generate a name automatically, About Storage facade, just see this. :)
I made function for upload file and if there is no uploaded file do it again till its upload
$imageFile->move($destinationFolder, $fileName);
if(file_exists(public_path('images') . '/' . $fileName))
return public_path('images') . '/' . $fileName;
else
self::uploadImage($imageFile, $fileName);
I used the code and it's working fine.
self::$image = $request->file('image');
self::$imageName = self::$image->getClientOriginalName();
self::$directory = 'category-images/';
self::$image->move(self::$directory, self::$imageName);
return self::$directory.self::$imageName;
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.
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
If I delete file like this :
$destinationPath = public_path() . DIRECTORY_SEPARATOR . 'img'. DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . $id;
$result = File::delete($destinationPath . DIRECTORY_SEPARATOR . $filename);
It works
But if I delete folder like this :
$destinationPath = public_path() . DIRECTORY_SEPARATOR . 'img'. DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . $id;
File::delete($destinationPath);
It does not work
Why delete folder does not work?
How can I solve this problem?
You are attempting to use the wrong method. You need to use deleteDirectory and not delete:
$destinationPath = public_path('img/products/'. $id);
File::deleteDirectory($destinationPath);
If you want to delete folder with files than use:-
use Illuminate\Support\Facades\Storage;
Storage::deleteDirectory($public_path);
I am trying to upload a file using laravel Storage i.e
$request->file('input_field_name')->store('directory_name'); but it is saving the file in specified directory with random string name.
Now I want to save the uploaded file with custom name i.e current timestamp concatenate with actual file name. Is there any fastest and simplest way to achive this functionality.
Use storeAs() instead:
$request->file('input_field_name')->storeAs('directory_name', time().'.jpg');
You can use below code :
Use File Facade
use Illuminate\Http\File;
Make Following Changes in Your Code
$custom_file_name = time().'-'.$request->file('input_field_name')->getClientOriginalName();
$path = $request->file('input_field_name')->storeAs('directory_name',$custom_file_name);
For more detail : Laravel Filesystem And storeAs as mention by #Alexey Mezenin
Hope this code will help :)
You also can try like this
$ImgValue = $request->service_photo;
$getFileExt = $ImgValue->getClientOriginalExtension();
$uploadedFile = time()'.'.$getFileExt;
$uploadDir = public_path('UPLOAS_PATH');
$ImgValue->move($uploadDir, $uploadedFile);
Thanks,
Try with following work :
$image = time() .'_'. $request->file('image')->getClientOriginalName();
$path = base_path() . '/public/uploads/';
$request->file('image')->move($path, $image);
You can also try this one.
$originalName = time().'.'.$file->getClientOriginalName();
$filename = str_slug(pathinfo($originalName, PATHINFO_FILENAME), "-");
$extension = pathinfo($originalName, PATHINFO_EXTENSION);
$path = public_path('/uploads/');
//Call getNewFileName function
$finalFullName = $this->getNewFileName($filename, $extension, $path);
// Function getNewFileName
public function getNewFileName($filename, $extension, $path)
{
$i = 1;
$new_filename = $filename . '.' . $extension;
while (File::exists($path . $new_filename))
$new_filename = $filename . '_' . $i++ . '.' . $extension;
return $new_filename;
}