views:

71

answers:

3

Hello

I am facing problem in downloading any image file from the server.

I am able to upload them successfully and can open them from the location where they are uploaded and stored.

When i download using my function the image files get downloaded fully, file size is also correct but when i open them i get an error No image preview !!!

$fileString=$fileDir.'/'.$fileName; // combine the path and file
 // translate file name properly for Internet Explorer.
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
   $instance_name = preg_replace('/\./', '%2e', $instance_name, substr_count($instance_name, '.') - 1);
}
// make sure the file exists before sending headers

if(!$fdl=@fopen($fileString,'r'))
 {
    die("Cannot Open File!");
 } 
else 
 {
   header("Cache-Control: ");// leave blank to avoid IE errors
   header("Pragma: ");// leave blank to avoid IE errors
   header("Content-type: application/octet-stream");
   header("Content-Disposition: attachment; filename=\"".$instance_name."\"");
   header("Content-length:".(string)(filesize($fileString)));
   sleep(1);
   fpassthru($fdl);
 }

I am using IE as browser.

I am using this code snippet to download the file and not to show on the browser. The script executes and i get prompted on whether i want to open / save the file. When i save the file the size is also correct but the image doesn't show up. When i right click and see the summary of the file, it says the summary is unavailable.

Thanks in advance. Kindly help.

A: 

This line: header("Content-type: application/octet-stream"); seems fishy to me. You might want to try giving the actual mime type and see if that helps

dnagirl
+1  A: 

It's not clear from your code, are you using this PHP snippet to serve the image on a web page, such as:

<img src="my-php-script.php" alt="blah blah blah" />

If so, your content-type is incorrect. You would need to use an image MIME type, such as image/gif, image/jpeg, image/png or image/tiff, whichever is most appropriate.

Kyle Smith
A: 

The problem is with the MIME type. Please have a look here:

http://www.daniweb.com/forums/thread122055.html

This is if you want to view images in the browser.

If you want to download them as binaries then leave your mime type.

Also please change file open mode from "r" to "rb"

DmitryK