Change name of uploading file into s3 - php

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');

Related

How can I convert base64 string to image and upload the file in storage and save the path in the database

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)

How to store image path in database - using laravel

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'

Uploading a base64 image as an image file

So I'm receiving an image in base64 encoding. I want to decode the image and upload it in a specific directory. The encoded file is an image and I'm using $file = base64_decode($base64_string); to decode it. Uploading it via file_put_contents('/uploads/images/', $file); always results in failed to open stream: No such file or directory error.
So, how do I take the decoded string, and upload it on a specific path?
Thanks.
Your path is wrong.
/uploads/images
is checking for a folder in the / directory called uploads. You should specify the full path to the folder:
/var/www/vhosts/MYSITE/uploads/images
I had the same problem and finally solved using this:
<?php
$file = base64_decode($base);
$photo = imagecreatefromstring($file);
//create a random name
$RandomStr = md5(microtime());
$name = substr($RandomStr, 0, 3);
$name .= date('Y-m-d');
$name .= '.jpg';
//assign name and upload your image
imagejpeg($photo, 'images/'.$name, 100);

How to add an image from url to temp?

I am using the SimpleImage.php class from: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/ , to resize, compress and save the images.
Usually using it with an image from input:
$tmp_dir = $_FILES['file']['tmp_name'];
$file_name = 'something.jpg';
include('SimpleImage.php');
$image = new SimpleImage();
$image->load($tmp_dir);
$image->resizeToWidth($width);
$image->save('imgd/l'.$file_name);
But how can I deal just with the image url? (from another website)
$img = file_get_contents($url);
The above $img variable keeps the actual image.
So how can I save it to temp to use it?
If this is the right way.
If it's possible not to have to change SimpleImage.php class.
With tempnam()
Creates a file with a unique filename, with access permission set to
0600, in the specified directory. If the directory does not exist,
tempnam() may generate a file in the system's temporary directory, and
return the name of that.
<?php
$tmpfname = tempnam("/tmp", "UL_IMAGE");
$img = file_get_contents($url);
file_put_contents($tmpfname, $img);
include('SimpleImage.php');
$image = new SimpleImage();
$image->load($tmpfname);
$image->resizeToWidth($width);
$image->save('imgd/l'.$file_name);
?>

PHP file-upload not working

I'm having trouble moving the file. I'm also wondering if there's a way for a user to upload a different picture, and have it automatically remove the old one?
move_uploaded_file(#$_FILES["profilepic"]["tmp_name"],"userdata/".$_POST['username']."/profilepic".$_FILES["profilepic"]["name"]);
You have remove # before $_FILES and check error.
if you want to replace picture old to new then make your own user wise image name and use it like.
$_POST['username']=test;
$imagname=$_POST['username']."_img";
move_uploaded_file(#$_FILES["profilepic"]["tmp_name"],"userdata/".$_POST['username']."/profilepic/".$imagename);
Try this block of code
$imagename = $_FILES['profilepic']['name'];
$source = $_FILES['profilepic']['tmp_name'];
$imagename = str_replace(" ", "_", $imagename);
$target = "userdata/".$_POST['username']."/profilepic" . $imagename;
move_uploaded_file($source, $target);
Make sure you have got write permission to the folder userdata,plmexico,profilepic.
Good habits:
Rename the user's file with a name generated by your algorithm. As you want to replace the profile pic when they upload a new picture you naming can combine user_id with the file name. So the code becomes.
$localname = $_FILES['profilepic']['name'];
$imagename = $user_id."_profile".explode(end(",",$localname)); // $user_id is the user's id
$source = $_FILES['profilepic']['tmp_name'];
$imagename = str_replace(" ", "_", $imagename);
$target = "userdata/".$_POST['username']."/profilepic" . $imagename;
move_uploaded_file($source, $target);
This code doesn't work if the user uploads an image in different format.

Categories