Viewed   69 times

I am developing mobile app which talks with server via PHP Webservice. This is my first time using PHP. I managed to upload data in to database. Now i need to send an image to store it in ftp server. For that i converted image->hex and sent from my app.

Server Side

I got the hex code but not sure how to convert it in to an image and store in in ftp server. I am really struggling here. I googled it but couldn't find exact one.

Any help is much appreciated.



Convert the HEX string to binary:

$binary = pack("H*", $hex);

pack("H*", ...) is equivalent to hex2bin, which is available since PHP 5.4.

Write it to disk:

file_put_contents("file.png", $binary);
Monday, December 26, 2022

If all you want to do is show the image to the user, you simply need to serve the right content header with the response:

if      (substr($string, 0, 4) == "x89PNG")  header('Content-Type: image/png');
else if (substr($string, 0, 2) == "xFFxD8") header('Content-Type: image/jpeg');
else if (substr($string, 0, 4) == "GIF8")     header('Content-Type: image/gif');

echo $string;

An image is really just a blob of data (like the 'string' you have) plus an indication that it's an image (in HTTP, that would be the MIME header). Since you already have the blob, all you need is the header to make the browser interpret it as an image.

Wednesday, October 19, 2022

I found the solution. Set the args this way. It works!

$args = array(
    'MessageAttributes' => [
        'AWS.SNS.SMS.SenderID' => [
               'DataType' => 'String',
               'StringValue' => 'YourSenderName'
    "SMSType" => "Transactional",
    "PhoneNumber" => "+87654321",
    "Message" => "Hello World!"
Wednesday, November 30, 2022

Normally, if you just use a prepared statement in place of a plain query, it's marginally slower since the query is prepared and executed in two steps instead of one. Prepared statements become faster only when you're preparing the statement and then executing it multiple times.

However, in this case you're using mysql_real_escape_string, which does a roundtrip to the database. Even worse, you're doing it inside a loop, so, executing it multiple times per query. So, in this case replacing all of those roundtrips with a single prepared statement is a win-win-win.

Regarding your last question, there's no reason you can't use the same query with a prepared statement as you would through the normal query parser (i.e. no reason to execute one version with an IN and the other with a bunch of ORs). The prepared statement can have IN (?, ?, ?), and then you just bind that number of parameters.

My advice would be to always use prepared statements. In cases where they add a marginal performance overhead, they're still worth it for the security (no SQL injection) and readability benefits. For sure, anytime you find yourself resorting to mysql_real_escape_string, you should use a prepared statement instead. (For simple one-off queries where there's no need to escape variable inputs, they aren't strictly necessary.)

Tuesday, December 20, 2022

You can download wkhtmltoimage from this link. There is a version for all operating systems so that shouldn't be a problem. Then you can use it like so:

$path="wkhtmltoimg/wkhtmltoimage.exe"; //path to your executable
shell_exec("$path $url $output_path");

One thing you want to note is that if PHP is in safe mode, shell_exec will not work and you won't be able to do your conversion.

Tuesday, August 9, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :