currently i can store file name is database and in public folder of laravel but i want to store file path in database?
My Controller:
$image=$userProfile['image'];
$image1 = base64_decode($image);
$filename = time().'.'.$extension;
Storage::disk('public')->put($filename,$image1);
$this->user->where('id', Auth::user()->id)->update(['profile_pic' => $filename]);
How i can store image path in laravel and image name in public folder? i am sending image in json using base64.
Your help will be highly appreciated?
Use:
$image = $userProfile['image'];
$image1 = base64_decode($image);
$filename = time().'.'.$extension;
Storage::disk('public')->put($filename,$image1);
$filePath = Storage::disk('public')->getAdapter()->getPathPrefix();
$this->user->where('id', Auth::user()->id)->update(['profile_pic' => $filePath.$filename]);
First type this command :
php artisan storage:link
it will create a symlink to public folder.
in your controller, you can write like this :
if ($request->file('image')) {
$file = $request->file('image')->store('images', 'public');
$new_product->image = $file;
}
this code will insert a random string included the path, so you can have something like this on your image column value 'images/shgsqwerfsd.jpg'
Related
I am building an SPA. I am getting base64 string from my vue component in my laravel controller. How can I convert base64 string to a file. Then upload the site into my laravel local storage and then save the path in the database table ?
I have googled almost every link and tried too many things. I even tried image intervention but the only thing I was able to achieve was to upload the file into the laravel storage but wasn't able to store the right path in the database table.
Here is my controller code
$team = new Team;
$team->hood_id = $user->hood->id;
$team->title = $request->title;
$team->max_no_of_players = $request->max_no_of_players;
$team->description = $request->description;
$team->about_us = $request->about_us;
$team->email = $request->email;
$team->contact_no = $request->contact_no;
$team->meetup_place = $request->meetup_place;
/**
* Image base64 converting code starts from here
*/
$image = $request->image;
preg_match("/data:image\/(.*?);/",$image,$image_extension); // extract the image extension
$image = preg_replace('/data:image\/(.*?);base64,/','',$image); // remove the type part
$image = str_replace(' ', '+', $image);
$imageName = 'image_' . time() . '.' . $image_extension[1]; //generating unique file name;
$team->image = Storage::disk('public')->put($imageName, base64_decode($image));
I should be able to use Storage::url($image); when fetching the teams from database table. But right now I am not getting the correct path, Any help would be highly appreciated
In this way, you can fetch the file from the Base64 URL, I used this way.
$pos = strpos($fileBase64Url, ';');
$filetype = explode('/', substr($fileBase64Url, 0, $pos))[1]; //get file type
$contents = file_get_contents($fileBase64Url); //get the content from the URL
$unique_name = 'filename.' . $filetype; //file name
Storage::put('/public/folderName/' . $unique_name, $contents); //save to the directory
//$unique_name (Save this name to the Database)
And this is the path of this image file
http://127.0.0.1:8000/storage/folderName/filename.jpg (remove public form url)
I get an image with format String base64 and i want to decode that string to be an image and save it to public folder in laravel.
This is my controller:
//decode string base64 image to image
$image = base64_decode($request->input('ttd'));
//create image name
$photo_name = time().'.png';
$destinationPath = public_path('/uploads');
//save image to folder
$image->move($destinationPath, $photo_name);
$img_url = asset('/uploads'.$photo_name);
$data = new Transaction();
$data->transaction_id = $request->input('fa_transaction_id');
$data->user_id = $request->input('userid');
$data->photo_name = $photo_name;
$data->photo_url = $img_url;
$data->save();
when i'm trying echo $image, i got the decode value, and also for $photo_name i got the value too, but when the function running i got this error
Call to a member function move() on string
how to fix this error?
//Controller
use Illuminate\Support\Facades\Storage;
//Inside method
$image = $request->image; // your base64 encoded
$image = str_replace('data:image/png;base64,', '', $image);
$image = str_replace(' ', '+', $image);
$imageName = str_random(10) . '.png';
Storage::disk('local')->put($imageName, base64_decode($image));
Also, make sure that your local disk is configured like that in /config/filesystems.php
'local' => [
'driver' => 'local',
'root' => storage_path('app/public'),
]
With that, file will be saved in /storage/app/public directory.
Don't forget to write php artisan storage:link to make files from that directory available in /public directory, so users will can retrieve them.
Saving image in database not a recommanded way to store image in db, reason is speed increase of memory, The correct way is just save the link to it.
Anyway, you can save your image in base64 with these line of code.
Get the correct image from path in your server not from link in the $path.
<?php
// Base 64 transform image
$path = __DIR__ .'/myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
echo $base64;
I use the below code to store an image to my S3 disk. But using this code i get a random name i.e filename to the image that I'm uploading. So how can i set the filename of $request->renter_proof while uploading? Someone please help.
$storagePath = Storage::disk('s3')
->put('/company/'.Auth::user()->company.'/renter-proof/',
$request->renter_proof,
'public');
I want the name of the file to be :
$imageName = 'Tag '.$request->input('stock_on_rent').'.'.$request->renter_proof->getClientOriginalExtension();
The solution is use putFileAs()
I used the below code:
$path = "/company/".Auth::user()->company."/renter-proof";
$imageName = 'Tag '.$request->input('stock_on_rent').'.'.$request->renter_proof->getClientOriginalExtension();
$storagePath = Storage::disk('s3')->putFileAs($path,$request->renter_proof,$imageName ,'public');
I already given 777 permission to my images folder. Charts Table is also saving all record of image. I am going to attach table:charts structure here.
public function store(Request $request)
{
$input = $request->all();
$tradeID= Auth::user()->trade()->create($input);
if($file = $request->file('file'))
{
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = Chart::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
$tradeID->chart()->create($input);
}
Try to change destination path from relative to absolute
public function store(Request $request)
{
$input = $request->all();
$tradeID= Auth::user()->trade()->create($input);
if($file = $request->file('file'))
{
$name = time() . $file->getClientOriginalName();
$file->move( public_path() . '/images/', $name); // absolute destination path
$photo = Chart::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
$tradeID->chart()->create($input);
}
try this
if($file = $request->file('file'))
{
$name = time() . $file->getClientOriginalName();
$path = $file->storeAs('images', $name,'public');
$photo = Chart::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
you can retreive the file with /storage/public/file_name or /file_name depending on where you told laravel to store the public files
You are not actually storing the file, you are actually moving a non-existent file.
What you might consider
You, most probably want to access uploaded file from public path. If so, you should store this file to particular folder in your storage directory and create a symlink to that directory in your public path.
Here's an example:
Storage::disks('public')->put($filename, $uploadedFile); // filename = 'images/imageFile.jpg'
This creates a file in your storage/app/public/images directory.
Then, you can create a symlink, laravel provide a simple command to do so.
php artisan storage:link
This creates a symlink to your public disk.
Then, you can access the image at http://your-site-server/storage/images/imageFile.jpg
Hope it helps.
$name = time().'.'. $file->getClientOriginalName(); //depends on ur requirement
$file->move(public_path('images'), $name);
Are you trying to move uploaded file or existing file ?
To move file you need to do:
File::move($oldPath, $newPath);
the old path and new path should include full path with filename (and extension)
To store an uploaded file you need to do:
Storage::disk('diskname')->put($newPath, File::get($file));
the new path is full path with filename (and extension).
disk is optional , you can store file directly with Storage::put()..
Disks are located in config/filesystems.php
The File::get($file) , $file is your input from post request $file = $request->file('file') , basically it gets contents of uploaded file.
UPD#1
Okay , you can do this:
$request->file('file')->store(public_path('images'));
Or with Storage class:
// get uploaded file
$file = $request->file('file');
// returns the original file name.
$original = $file->getClientOriginalName();
// get filename with extension like demo.php
$filename = pathinfo($original)['basename'];
// get public path to images folder
$path = public_path('images');
// concat public path with filename
$filePath = $path.'/'.$filename;
// store uploaded file to path
$store = Storage::put($filePath, File::get($file));
I want to upload an image and retrieve in from database when i need this.My code is perfectly working to upload image but it's path did not save in database.In database it's showing Null.Here is my code:
if ($request->hasFile('profilePic')) {
$file = array('profilePic' => Input::file('profilePic'));
$destinationPath = 'img/'; // upload path
$extension = Input::file('profilePic')->getClientOriginalExtension();
$fileName = rand(11111,99999).'.'.$extension; // renaming image
Input::file('profilePic')->move($destinationPath, $fileName);
}else{
echo "Please Upload Your Profile Image!";
}
$profile->save();
My Question:
How to save image path into database?
And how to retrieve image from database?
Updated: Profile.php
class Profile extends Model
{
protected $table = "profiles";
public $fillable = ["firstName","lastName","middleName","DOB","gender","featuredProfile","email","phone","summary","profilePic"];
}
In your code there should be like this:
$profile->profilePic = $fileName;
& then
$profile->save();
1.How to save image path into database?
To save your full image path in a database field, the code is:
$profile->profilePic = $destinationPath.$fileName;
$profile->save();
2.And how to retrieve image from database?
In your view file using blade engine, write HTML code like this. In the image src you have to provide profilePic data as shown in the below HTML code:
<img src="{{asset($profile->profilePic)}}"/>
try this
$path = $file->getRealPath();;
$pos = strpos($path,'/public/');
if ($pos !== false) {
$path = substr($path, $pos + 1);
}
$input['file'] = $path;