当前位置: 动力学知识库 > 问答 > 编程问答 >

php file force download error

问题描述:

I am trying to force a download. here is my code:

 <?PHP

// Define the path to file

$file = 'http://myimage.com/users/test/uploads/1234.png';

if(!file)

{

// File doesn't exist, output error

die('file not found');

}

else

{

header('Content-disposition: attachment; filename='.$file);

header('Content-type: application/octet-stream');

readfile($file);

}

?>

here is the error that I get in firefox: "The image "file:///C:/Users/Username/Downloads/1234%20(4).png" cannot be displayed because it contains errors."

the %20(4) is a space, and probably the 4th copy i've tried downloading.

网友答案:

Your Content-Type is wrong. All that's needed to force a download is Content-Disposition: attachment; change it to image/png instead of application/octet-stream.

Also, you have a space before your opening <?php tag. You need to take that out.

Also, your "file exists" check doesn't do what you think it does. Use file_exists to check if a file exists, and remember the $ sign.

网友答案:

Ensure you don't have any blank lines outside the <?php ?> tags.

分享给朋友:
您可能感兴趣的文章:
随机阅读: