聘我网

新概念招聘3.0

点击一个链接,如何分别产生查看和下载的效果?

vote up0vote downstar

就像gmail的附件一样,你可以直接查看,也可以下载,这个功能用PHP如何实现呢?

 

1 个答复

vote up0vote downcheck

下载部分可以这样实现:

$filename = 'qas.jpg';
function download($filename)
{
    if (!is_file($filename)) {
      die('The file appears to be invalid.');
    }

    $filepath = str_replace('\\', '/', realpath($filename));
    $filesize = filesize($filepath);
    $filename = substr(strrchr('/'.$filepath, '/'), 1);
    $extension = strtolower(substr(strrchr($filepath, '.'), 1));

    $mime = array('application/octet-stream');

    header('Content-Type: '.$mime[0]);
    header('Content-Disposition: attachment; filename="'.$filename.'"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.sprintf('%d', $filesize));
    header('Expires: 0');

    // check for IE only headers
    if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)) {
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Pragma: public');
    } else {
      header('Pragma: no-cache');
    }

    $handle = fopen($filepath, 'rb');
    fpassthru($handle);
    fclose($handle);
}
download($filename);

浏览部分:

function view($filename)
{
    header("Content-Type: " . finfo_file(finfo_open(FILEINFO_MIME), $filename));
    readfile($filename);
}

PHP5.2.5会碰到些问题,参考这里(搜索christophe的帖子)

链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.