Question

I cant seem to base encode and save an image. I have stripped the base64 but still no joy.

Output

$random echo = 838e8afb1ca34354ac209f53d90c3a43jpg

Array
    (
        [0] => Array
            (
                [id] => 0
                [storename] => test
                [notes] => test
                [image] => 503316
            )

    )

PHP

$random = md5(rand()).".jpg";

echo $random;

$image = preg_replace('#^data:image/[^;]+;base64,#', '', $data['image']);

$nonerejected[] = array(
             'id' => $data['id'],
             'storename' => $data['storename'],
             'notes' => $data['notes'],
             'image' => $data['image'] == "" ? "" : file_put_contents($random,base64_decode($image)),   
                );

If I echo $image

9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2..goes on for some..

Any ideas?

Was it helpful?

Solution

Put outside array .

            $random = md5(rand()).".jpg";
            $image = preg_replace('#^data:image/[^;]+;base64,#', '', $data['image']);

                file_put_contents($random,base64_decode($image))    ;

                $nonerejected[] = array(
                    'id' => $data['id'],
                    'storename' => $data['storename'],
                    'notes' => $data['notes'],
                    'image' => $random, 

                );
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top