开发者

PHP downloading script

开发者 https://www.devze.com 2023-04-10 02:40 出处:网络
I\'m trying to write a php script that will开发者_开发知识库 check parameters passed in before it initiates a download to the client. I\'ve started by attempting to just initiate a download:

I'm trying to write a php script that will开发者_开发知识库 check parameters passed in before it initiates a download to the client. I've started by attempting to just initiate a download:

    <?php
    $file =  '/tticon.jpg';

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header("Content-Disposition: attachment; filename=\"".basename($file)."\"");
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
?>

When I navigate to this script it shows a blank page and nothing happens. How do I initiate a download?


Try using application/force-download as Content-Type. If you want to show the image in the brwoser you can use image/jpg.


header("Content-type: application/force-download"); 


Since the file's extension is ".jpg" I assume it's mime is image/jpeg.

Replace this line:

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

with this:

header('Content-Type: image/jpeg');

If you really want to force the browser to download the file (which I find very unlikely, anyway):

Replace the line with this:

header("Content-Type: application/force-download");


Your $file looks suspect. Do you really have a tticon.jpg in the root directory of your server? Remember that PHP's file-functions operate on the FILESYSTEM of the server, not the WEB directories that Apache presents. PHP will not magically pre-pend your site's document root to that path. It's literally going to be looking in the root directory of the server's file system, NOT in the document root of your site.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号