Data Image base64 Upload to Directory

staffing

New Member
Messages
18
Reaction score
0
Points
1
Hey I wanted to know how to convert a base64 data:image to a link on my website, I have this article poster and when you upload images it'll use the base64 link instead of uploading automatically, so I wanted to create a function that would grab the base64 link, covert it/upload it to my images directory and then push it all to my database

The current problem I'm having is simply converting the base64 to an image and uploading it, here is my code so far which i found on stackoverflow:
<?php
function base64_to_img($base64_string, $output_file) {
$output_file = $_GET['filename'];
$output_dir = "/images/";
$ifp = fopen($output_dir.$output_file, "wp");

$data = explode(',', $base64_string);

fwrite($ifp, base64_decode($data[1]));
fclose($ifp);

return $output_file;
}
$string = 'data:image/png;base64,BASE64/IMAGE/URI/HERE'; // Too long to actually paste it all in here
base64_to_img($string, $output_file);
?>​
My php error log is returning "Failed to open stream" - no such file/directory
It's also returning expects parameter 1 to be resource, boolean given; That is simply a short version of what I need to do, I need to run this script within this function:
$doc = new DomDocument();
$doc->loadHTML($content);
$sub = $doc->getElementsByTagName("img");
foreach ($sub as $sub) {
$src = $tag->getAttribute('src');

}​
Is this at all possible?
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
Your output directory is incorrect. You want to use ./images. using /images refers to the root of the server, which you don't have access to read/write to.

That should fix your issue. :)
 

staffing

New Member
Messages
18
Reaction score
0
Points
1
Your output directory is incorrect. You want to use ./images. using /images refers to the root of the server, which you don't have access to read/write to.

That should fix your issue. :)
Fixed! Thank you very much kind sir!
 
Last edited:
Top