0

I'm writing image files from a base64 encoded string with a php webservice in this very same folder using file_put_contents(). Here is the writing part [PHP]:

$data = base64_decode( $datastring );
$res = file_put_contents( $path, $data );
$resultf = array();
$resultf["request"] = "setpic";
$resultf["result"] = strval($res);
$this->sendResponse(200, json_encode($resultf));

And the reading part follows [PHP]:

$datastring = file_get_contents( $path );
$data = base64_encode( $datastring );
$resultf["request"] = "getpic";
$result0 = array();
$result0["path"] = $path;
$result0["data"] = $data;
$resultf["result"] = $result0;
$this->sendResponse(200, json_encode($resultf));

The two $path are correct.

Nevertheless although the files are "correctly" created (I can see them with ssh with a size of approx. 9kB), if I scp the image files they appear to have a width and height of 0, ie a corrupted image file I presume. If I try to load them using XCode in my iOS app, I get the following error:

ImageIO: <ERROR>  JPEG Corrupt JPEG data: 75 extraneous bytes before marker 0xdb

Please note that containing folders have 777 permissions, same user/group than the php webservice accessing them form a parent folder. This method worked until I unfortunately changed the owner of the root folder.

Thanks a lot for your help.

ibanez
  • 158
  • 1
  • 1
  • 8
  • how are you writing the file? it seems you can create it, but you are not writing nothing on it – Gianpaolo Di Nino Aug 28 '12 at 21:49
  • 6
    Please provide some code so we have a better idea of what you're talking about. – Matt Aug 28 '12 at 21:53
  • I am writing the files using the PHP file_put_contents() from a decoded base64 string. The post has been edited for more details, sorry about the vagueness – ibanez Aug 29 '12 at 08:01
  • Provide more code, and please can you `echo` what is in your `$path` – pomaxa Aug 29 '12 at 08:21
  • Thanks for your interest! The issue is clearer now. – ibanez Aug 29 '12 at 09:00
  • The problem will be with your $datastring rather than the file_put_contents or the json_encode/json_decode if you say scp'ing the saved file doesn't work when you try to open it. Your $path should include the .jpg file extension depending what program are you trying to use to open it to test this. We need to go back a step and understand where you are getting your $datastring from as it's more than likely some characters have not been encoded correctly (perhaps by URL encoding etc.) – JamieL Mar 09 '14 at 22:48

0 Answers0