26

I know how to upload image file and save to other location by using the following code. However, I need to do in such a way that user upload image and automatically convert to base64 without saving that image in my location. How should I do?

<?php
//print_r($_FILES);
if(isset($_FILES['image']))
{
    $errors=array();
    $allowed_ext= array('jpg','jpeg','png','gif');
    $file_name =$_FILES['image']['name'];
 //   $file_name =$_FILES['image']['tmp_name'];
    $file_ext = strtolower( end(explode('.',$file_name)));


    $file_size=$_FILES['image']['size'];
    $file_tmp= $_FILES['image']['tmp_name'];
    echo $file_tmp;echo "<br>";

    $type = pathinfo($file_tmp, PATHINFO_EXTENSION);
    $data = file_get_contents($file_ext);
    $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
    echo "Base64 is ".$base64;



    if(in_array($file_ext,$allowed_ext) === false)
    {
        $errors[]='Extension not allowed';
    }

    if($file_size > 2097152)
    {
        $errors[]= 'File size must be under 2mb';

    }
    if(empty($errors))
    {
       if( move_uploaded_file($file_tmp, 'images/'.$file_name));
       {
        echo 'File uploaded';
       }
    }
    else
    {
        foreach($errors as $error)
        {
            echo $error , '<br/>'; 
        }
    }
   //  print_r($errors);

}
?>


<form action="" method="POST" enctype="multipart/form-data">

<p>
    <input type="file" name="image" />
    <input type="submit" value="Upload">

</p>
</form>
14
  • What do you want to happen after the image is converted to base64? Commented Oct 12, 2013 at 15:03
  • actually, for project, I want to save that base64 into database instead of saving image file. Commented Oct 12, 2013 at 15:13
  • 4
    Use file_get_contents() to get the contents of the temporary uploaded file, base64_encode() to encode it, file_put_contents() to save the file. Although storing a base64 encoded representation of an image file sounds like a bad idea - with big files, you may run into RAM problems, and the resulting file will be 33% larger than the original. Why do that? Commented Oct 12, 2013 at 15:16
  • Yes. I need to test with that one. If I writing like this, I can't get base64 string. Can you please guide me? $type = pathinfo($file_tmp, PATHINFO_EXTENSION); $data = file_get_contents($file_ext); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "Base64 is ".$base64; Commented Oct 12, 2013 at 15:23
  • 3
    file_get_contents($file_ext); you're not using $file_tmp Commented Oct 12, 2013 at 15:47

1 Answer 1

21

There is a mistake in your code:

$data = file_get_contents( $file_ext );

This should be:

$data = file_get_contents( $file_tmp );

This should solve your problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.