在PHP中下载文件pdf / jpg / png无效

时间:2015-09-13 14:34:17

标签: php pdf download png jpeg

我正在尝试使用PHP下载pdf / jpg / jpeg / png。这是我的代码

$result= mysqli_query("SELECT * FROM upload WHERE upload_id='$id'");
$row = mysqli_fetch_array($result);

//get file extension
$filename = $row['upload_name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);

if($ext=='jpg'||$ext=='jpeg'||$ext=='png' ||$ext=='pdf'){
echo "<a href=\"download.php?id={$row['upload_name']} \">Download</a>";
}

的download.php

$name= $_GET['id'];

header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=\"" . basename($name) . "\";");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($name));
ob_clean();
flush();
readfile("uploads/".$name); //showing the path to the server where the file is to be download
exit;

当我尝试上面的代码时,可以下载,但是当我尝试打开pdf / jpg / png时,它表示无效。我的代码有什么问题吗?这是我第一次使用下载代码。所以我真的不知道是什么问题。

对于此路径,如下面的代码所述,

readfile("uploads/".$name);

如果我的pdf / jpg / png文件保存在我的计算机C:\ Apache2.2 \ htdocs \ epals \ uploads

中,我该怎么写呢?

请帮助谢谢

0 个答案:

没有答案