下载部分可以这样实现:
$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的帖子)