Data Image base64 Upload to Directory

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by staffing, Mar 22, 2015.

  1. staffing

    staffing New Member

    Messages:
    17
    Likes Received:
    0
    Trophy 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?
     
  2. staffing

    staffing New Member

    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    1
  3. leafypiggy

    leafypiggy Manager of Pens and Office Supplies Staff Member

    Messages:
    3,819
    Likes Received:
    162
    Trophy 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. :)
     
  4. staffing

    staffing New Member

    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    1
    Fixed! Thank you very much kind sir!
     
    Last edited: Mar 30, 2015

Share This Page