php : file_put_contents error - php

I'm using file put content to save my image to the server, but I get an error and the image is not saved .
I am getting the base64 string from android app, I decode it and pass it on for saving. My php script is in this location (example):
www.aaa.com/test
I want to save the photos to:
www.aaa.com/test/photos
What I have:
$name = "image123.png";
$data = base64_decode($fldPostPhoto);
file_put_contents("photos/".$name,$data);

Related

How to upload filepond image to MySql with PHP?

I have included the javascript in my project and FilePond works great on the FrontEnd, as you can see below:
Here is the code in my HTML:
When I submit the form this is what I get for filepond:
Code:
https://snipsave.com/user/viktorgavrilovic/snippet/TpgWPYaBBmWe1N44YO/
Now, how can I save the image to a folder, let's say: C:/temp/?
I know that I have to decode because the data is base64 encoded, here is my PHP so far, with decoded format:
$filepond = $_POST["filepond"];
$json_decoded = json_decode($filepond[0], true);
var_dump($json_decoded);
and this is what I get from the decoded format:
Code:
https://snipsave.com/user/viktorgavrilovic/snippet/u3Th8eqrzvmq6IAWAI/
Could you please tell me what should I do next in order to save the image in a folder and in a MySql DB? Thank you very much!

Laravel decode base64 image and hashName and store public folder

How I can store decoded image to laravel public image path and hash image name ?
I tried like this, but didnt work and doesn't get any error message;
if (!empty($request->image)) {
$file = base64_decode($request->image)->hashName();
base64_decode($request->image)->store('user-uploads/avatar');
asset('user-uploads/avatar/' . $file);
}
This code works fine but I use API and via curl sending image then needed base64 decoding:
if ($request->hasFile('image')) {
$file = $request->file('image')->hashName();
$request->image->store('user-uploads/avatar');
asset('user-uploads/avatar/' . $file);
}
What you suggest save image and send image url to api and downloads image too path or send base64 image encode and in api decode, like I'm trying at the moment?
You can't call functions on the output of base64_decode, as it returns as string.
I would suggest you post the name of the file along with the base 64 encoded content, which can then be saved using laravel's storage helpers:
Storage::disk('local')->put('image.png', base64_decode($request->image))

how to convert base64 encoded image multi file formate in server side PHP

Hi i'm sending encoded base image string from android side and in server side i'm decoding the encoded image string and storing to folder, while saving to database i'm storing .JPG format.
But the issue is if i upload .PNG image from android side it is storing in .JPG format but image is not visible in server side folder, and if i upload .JPG format it is visible in server side folder. Please help me out this problem.
PHP Code
if(isset($_REQUEST['image1']))
{
$profile_picture = $_REQUEST['image1'];
$target_path = "./upload/"."file"."_".time().".jpg";
// **I want store multiple file forame** //
$upload_img= "file"."_".time().".jpg";
file_put_contents($target_path, base64_decode($profile_picture));
}
else
{
$upload_img = "image.png";
}
You can send file extension(.jpg, .png) in webservice from android side and retrieve it in your back-end PHP side then store it in variable and use it dynamically.

PHP base64 encode a pdf file

I am using an API where I can send a document to something like dropbox. According to the documentation, the file which is sent needs to be BASE64 encoded data.
As such, I am trying something like this
$b64Doc = chunk_split(base64_encode($this->pdfdoc));
Where $this->pdfdoc is the path to my PDF document.
At the moment, the file is being sent over but it seems invalid (displays nothing).
Am I correctly converting my PDF to BASE64 encoded data?
Thanks
base64_encode takes a string input. So all you're doing is encoding the path. You should grab the contents of the file
$b64Doc = chunk_split(base64_encode(file_get_contents($this->pdfdoc)));
base64_encode() will encode whatever string you pass to it. If the value you pass is the file name, all you are going to get is an encoded filename, not the contents of the file.
You'll probably want to do file_get_contents($this->pdfdoc) or something first.
Convert base64 to pdf and save to server path.
// Real date format (xxx-xx-xx)
$toDay = date("Y-m-d");
// we give the file a random name
$name = "archive_".$toDay."_XXXXX_.pdf";
// a route is created, (it must already be created in its repository(pdf)).
$rute = "pdf/".$name;
// decode base64
$pdf_b64 = base64_decode($base_64);
// you record the file in existing folder
if(file_put_contents($rute, $pdf_b64)){
//just to force download by the browser
header("Content-type: application/pdf");
//print base64 decoded
echo $pdf_b64;
}

saving image from a webpage + saving its name too using PHP

how can i save an image from a web page , for example:
http://othersdomain.com/image/colorful.jpg
How can i save the image and save it to my directory "image" inside FTP.
and also get the "colorful" name from the link.
As a conclusion , it will get colorful.jpg and save in my directory with the same name and type then echo out the link to it(on my web server).
after being saved to my server , it will echo out somehting like this:
http://mydomain.com/image/colorful.jpg
You can get a file from another server using file_get_contents.
$file = file_get_contents('http://othersdomain.com/image/colorful.jpg');

Categories