PHP>打开文件而不是强制下载

时间:2011-01-13 19:53:18

标签: php download

我正在尝试将我购买的脚本修改为浏览器中的“OPEN”文件,而不是像目前那样将“FORCE DOWNLOAD”修改。我之前粘贴了代码。

以下是目前的工作方式。

  1. 用户购买Blackberry应用程序
  2. 带下载链接的电子邮件是 点击。
  3. 单击URL时,文件“DOWNLOADS”而不是 开放或执行。
  4. 我想制作网址>在用户的移动浏览器中打开而不是强制下载。当用户收到链接时,它看起来像这样:

    http://example.com/delivery/ds-download.php?auth=bKKun8%2BFqpqjo56j1G5SaWNjg3Sgopah2NGf05iXop2k26SRzMlyiXaDiXmWZ2Rwd2aXaaOWamSkpGmqaWqommWUbWaonnVqanVucJWlqKaUnMVznsufk8rPccWll9bGp5Gek9HNlaqklad0zZ6RoaFhxKTPomGXzsSc0HVvzdWV0JWg2NKWmqNuiXm5hWxnnqfGosHUp5%2FDyKWic5rJz5XVl6bIyXJmY2lpbZpnZm5qb5Ccx9SXpMLXmMh1b8nZoMyol6GWZm5mYGhnlmdldGSY2aXL2JdwnZKX066h0NCRx3Q%3D&file=install

    任何人都可以帮我更新一下这个OPENS而不是强行下载吗?

    <?php
    require_once('ds-config.php');
    
    if (isset($_GET['auth']) === TRUE && isset($_GET['file']) === TRUE) {
    
        if (($product = is_valid_download_request($_GET['auth'])) !== FALSE) {
            header('Pragma: public');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Cache-Control: private', FALSE);
            header('Content-Type: application/force-download');
            header('Content-Disposition: attachment; filename="' . basename($product[3]) . '"');
            header('Content-Transfer-Encoding: binary');
    
            if (strcmp(substr($product[3], 0, 7), 'http://') !== 0) {
    
                header('Content-Length: ' . filesize($product[3]));
    
            }
    
            readfile($product[3]);
    
            exit;
        }
    }
    ?>
    

    这是下载链接指向的PHP文件。按照下面的反馈,我现在收到文件打开时的错误。好消息是它不再是强行下载了。到目前为止,谢谢!

    <?php
    
    /* SETTINGs */
    
    $url_pcmac = "http://www.example.com";
    $url_iphone = "http://itunes.apple.com/us/app/";
    $url_otherphones = "http://example.com";
    
    $url_bb_42 = $url_otherphones;
    $url_bb_43 = $url_otherphones;
    $url_bb_45 = "http://example.mobi/F45/i.jad";
    $url_bb_46 = "http://example.mobi/F46/i.jad";
    $url_bb_47 = "http://example.mobi/F46/i.jad";
    $url_bb_50 = "http://example.mobi/F50/i.jad";
    $url_bb_60 = "http://example.mobi/F50/i.jad";
    
    
    /* ************************************* */
    /* DO NOT TOUCH ANYTHING UNDER THIS LINE */
    /* ************************************* */
    /* *UNLESS YOU KNOW WHAT YOU ARE DOING** */
    /* ************************************* */
    
    
    
    
    function checkPhone($str) {
        $check = stripos($_SERVER['HTTP_USER_AGENT'], $str);
        if ($check === false) {
            return false;
        } else {
            return true;
        }
    }
    
    /* REDIRECT IPHONE AND IPODS */
    if(checkPhone("iPhone") || checkPhone("iPod")){
        header("Location: ".$url_iphone);
        exit();
    }
    
    /* REDIRECT BLACKBERRY */
    if(checkPhone("BlackBerry")){
    
        $agent = $_SERVER['HTTP_USER_AGENT'];
        $osdir = ''; 
    
        if (eregi("BlackBerry", $agent)) {
    
            if (eregi("6.0", $agent))
                 header("Location: ".$url_bb_60);
            if (eregi("5.0", $agent))
                 header("Location: ".$url_bb_50);
            if (eregi("4.7", $agent))
                 header("Location: ".$url_bb_47);
            if (eregi("4.6", $agent))
                 header("Location: ".$url_bb_46);
            if (eregi("4.5", $agent))
                 header("Location: ".$url_bb_45);
            if (eregi("4.3", $agent))
                 header("Location: ".$url_bb_43);
            if (eregi("4.2", $agent))
                 header("Location: ".$url_bb_42);
        }
    
        exit();
    }
    
    /* REDIRECT ALL OTHER PHONES */
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $accept = $_SERVER['HTTP_ACCEPT'];
    
    if(checkPhone("opera mini")
    || checkPhone("android")
    || preg_match('/(pre\/|palm os|palm|hiptop|avantgo|fennec|plucker|xiino|blazer|elaine)/i',$user_agent)
    || preg_match('/(iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile)/i',$user_agent)
    || preg_match('/(mini 9.5|vx1000|lge |m800|e860|u940|ux840|compal|wireless| mobi|ahong|lg380|lgku|lgu900|lg210|lg47|lg920|lg840|lg370|sam-r|mg50|s55|g83|t66|vx400|mk99|d615|d763|el370|sl900|mp500|samu3|samu4|vx10|xda_|samu5|samu6|samu7|samu9|a615|b832|m881|s920|n210|s700|c-810|_h797|mob-x|sk16d|848b|mowser|s580|r800|471x|v120|rim8|c500foma:|160x|x160|480x|x640|t503|w839|i250|sprint|w398samr810|m5252|c7100|mt126|x225|s5330|s820|htil-g1|fly v71|s302|-x113|novarra|k610i|-three|8325rc|8352rc|sanyo|vx54|c888|nx250|n120|mtk |c5588|s710|t880|c5005|i;458x|p404i|s210|c5100|teleca|s940|c500|s590|foma|samsu|vx8|vx9|a1000|_mms|myx|a700|gu1100|bc831|e300|ems100|me701|me702m-three|sd588|s800|8325rc|ac831|mw200|brew |d88|htc\/|htc_touch|355x|m50|km100|d736|p-9521|telco|sl74|ktouch|m4u\/|me702|8325rc|kddi|phone|lg |sonyericsson|samsung|240x|x320vx10|nokia|sony cmd|motorola|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|psp|treo)/i',$user_agent)
    || isset($_SERVER['HTTP_X_WAP_PROFILE'])
    || isset($_SERVER['HTTP_PROFILE'])
    || (strpos($accept,'text/vnd.wap.wml')>0)
    || (strpos($accept,'application/vnd.wap.xhtml+xml')>0)){
        header("Location: ".$url_otherphones);
        exit();
    }
    
    /* REDIRECT TO PC and MAC */
    header("Location: ".$url_pcmac);
    exit();
    
    ?>
    

3 个答案:

答案 0 :(得分:3)

您只需设置适当的Content-Type,浏览器就应该处理其余内容。

例如,您目前正在使用...

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

这需要更改为正确的MIME类型(可能是“application / vnd.rim.cod”,但这是猜测)。

N.B。:您还需要删除“Content-Disposition”行。

答案 1 :(得分:2)

这是内容配置:附件;这告诉浏览器下载它。

尝试:

header('Content-Disposition: inline; filename="' . basename($product[3]) . '"');

代替。

答案 2 :(得分:0)

问题来自错误的标头值; 您使用:

header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="'.basename($product[3]).'"');

这应该替换为:

header('Content-Type: ' . $mimeType);
header('Content-Disposition: inline; filename="'.basename($product[3]).'"');

其中 $mimeType 应该是有效的媒体类型(MIME 类型)。此类型的列表 here 可用。