2

I am having a problem to output binary data by using PHP's header and can't figure out why, would you please help?

(the form)

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

(the PHP script)

<?php

require_once "connection.php";

$pic = $_FILES['fileToUpload'];

$pic_info = getimagesize($pic['tmp_name']);
$pic_mime_type = $pic_info['mime'];
$pic_data = file_get_contents($pic['tmp_name']);
$pic_size = $pic['size'];

header("Content-type: " . $pic_mime);
header("Content-length: " . $pic_size);

echo $pic_data;
?>

The output of the script is just an empty squre, would you please help?

Thanks a lot!

Kenny
  • 153
  • 1
  • 1
  • 9
  • 1
    Are You sure the upload actually succeeded? You don't check anything here. I suggest reading up [Handling File Uploads](http://php.net/manual/en/features.file-upload.php) manual section and adding a few checks here, this may give You an idea what went wrong. Also may be a good idea to use **[readfile](http://php.net/readfile)** instead of file_get_contents+echo – poncha Apr 13 '16 at 07:34
  • Make sure your file is uploaded and i am not sure about the 'size' key in your $pic array. Not sure that the size is returned by getimagesize() – Andreas Apr 13 '16 at 07:37
  • Thanks Poncha and Andreas, thanks to Martin, problem solved :) – Kenny Apr 13 '16 at 16:02

1 Answers1

0

You are using

header("Content-type: " . $pic_mime);

instead of

header("Content-type: " . $pic_mime_type);

and make sure that connection.php doesn't output anything, as it is parsed before the headers are sent.
http://www.php.net/manual/function.header.php

example of connection.php
bad

<html>
<?php
echo 'foo';
?>

good

<?php
$result = 'bar';
  • no HTML-code (or anything else before the opening <?php tag)
  • no echo (or print, or print_f, or anything that sends data back)
  • no closing ?> tag (and therefore no unintended whitespace after it)

Also take a look at How to fix "Headers already sent" error in PHP.

Community
  • 1
  • 1
  • Sorry Martin, typo :(, but even corrected it back to "$pic_mime_type", it still doesn't work, any idea? thanks for your help – Kenny Apr 13 '16 at 07:27
  • I tried it over here and it's working quite well. I just had to remove `require_once "connection.php";`, because I'm don't know what this is doing. Also it's possible that this is the error reason if it returns data (a blank after `?>` is enough) you can't send headers afterwards. That's why you shouldn't use close the php tag at all in include files. – Martin Hieden Apr 13 '16 at 07:33
  • Thanks very very much Martin, I have totally no idea what to do so I follow what you said, remove the "connection.php" and also remove the "?>" tag at the end. With or without the "?>" won't affect anything, but the "connection.php" does. It just a file of connecting the server and database. That is where the problem arise, do you know why it cause a problem? It's just a script that's about the server name, username, password and database name. Really thanks for you help. – Kenny Apr 13 '16 at 14:44
  • Thank you so much Martin, really appreciate :) Have a nice day! – Kenny Apr 13 '16 at 15:57
  • You're welcome. Mind voting on the answer and/or accepting it? – Martin Hieden Apr 13 '16 at 16:15