In a server sided script I am using the following PHP code to encode an image:
$handle = fopen($imageSrc,'r');
$file_content = fread($handle,filesize($imageSrc));
fclose($handle);
$encoded = base64_encode($file_content);
echo $encoded;
When I read the file through a webservice it has the length 9309 bytes which is an invalid length for base64 encoded data. Examining the data, I found out that the value last byte is 10 (LF). Right now I am cropping the last byte away in my webservices before decoding the data. Is there a clean way to avoid the LF character to be sent?
I’m not sure what is causing this, but having debugged similar issues in the past, I would assume that there is something below the
echocausing a\nto be output.To test this, put a
die();directly after theecho. If my theory is correct, you can either just leave thedie();, or (better) move it progressively downwards in the execution flow until you find whatever is causing the\nto be output.