0

I have the following code in PHP, and it works for the most, fine. I am sending a image from a mobile device to this script, which decodes it into a img file and creates a file out of it on the server. I am 99.9% sure every time its a base64 encoded.

<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: image/jpeg');

$data = ($_POST['imageData']);

define('UPLOAD_DIR', 'images/');

$img = str_replace('data:image/jpeg;base64,', '', $data);

$data = base64_decode($img);

$file = UPLOAD_DIR . uniqid() . '.jpg';

file_put_contents($file, $data);

echo ('{"imgUrl" : "' . $file . '"}');

?>

This then returns the image URL back to be added to a database.

The problem is, most of the time it does decode into a .jpg file, and other times into a txt file. I cannot see why it does it, as its a little random. But I have noticed that sometimes it will come as a $_POST, and other times, $_POST is Null. So I looked at using:

$data = json_decode(file_get_contents('php://input'));

But again, it seems inconsistant. But I put a logic statement such as:

$data = ($_POST['imageData']);
if($data == NULL) {
    $data = json_decode(file_get_contents('php://input'));
}

Is there any reason I should be aware of why the code works, and sometimes does not work ?

Andrew Walker
  • 492
  • 1
  • 5
  • 23
  • Your header is saying the output content type is a jpeg image, and then you output a string (and apparently hand-making a json string at that...). Have you bothered checking that the POST is working properly? If you get an actual .jpg image, you should be able to do `getimagesize($file)` and get some data about it. Right now you're just assuming everything will always work perfectly, which is a BAD thing to do with web-based code. – Marc B May 27 '14 at 20:54
  • okay, so the data coming is base64, and the file name is going out as a json. The file thats created still ends up as a .txt. do I need to remove the header ? Would that resolve it ? – Andrew Walker May 27 '14 at 20:56
  • there is no way your code could produce a `whatever.txt` file, since there's literally no `.txt` anywhere in your code. – Marc B May 27 '14 at 20:57
  • Yes, I have bothered! I have done varDumps of `$_POST` and sometimes it full and sometimes its NULL. In each case, via console, the data is being sent, and all of it. – Andrew Walker May 27 '14 at 20:58
  • hmm, well something is, because sometimes the images on the device work, and other times it does not, and when I see it, and look in the server folder, its file name is next to a .txt file. – Andrew Walker May 27 '14 at 21:01

1 Answers1

0

I know this question is old, but is one of the first appearance by looking at this topic. So everyone looking at this question can find a link to a proper answer right away.

You should check this PHP - get base64 img string decode and save as jpg (resulting empty image )

Also check the conditions you're using, because

if ($data === NULL)

it may be different for

if ($data == NULL)

Also, you're saving the base64 string incorrectly to an image file. Check that link and let me know how if it helped.

Koken
  • 102
  • 6
  • Hi @Koken, if there's already a question with an answer you shouldn't link that answer in a new answer, instead you should mark this question as a duplicate. Please consider removing this post and marking the question as duplicate. – toti08 Sep 24 '18 at 14:57
  • Hi @toti08 , thank you for the clarification. I can't set the question as duplicated due to my low reputation. Do you recommend me to delete my answer anyway? – Koken Sep 24 '18 at 15:33