每次我使用此代码并下载文件时,都会出错。我认为这是因为文件的路径。
download.php文件位于/ modules /中,并包含在root(在htdocs / project /)内部的index.php中。该文件位于/modules/download/file.zip
可能是什么原因? URL应该是系统路径?如果我不想使用系统路径怎么办?或者我的另一个问题是什么?
$stmt = $this->GetDatabaseConnection()->prepare('SELECT * FROM download_files WHERE id=?');
$stmt->bind_param('i', $_GET['file']);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id, $name, $desc, $filename, $size, $added, $clicks, $uclicks);
$stmt->fetch();
if($stmt->num_rows > 0) {
$fakeFileName= $name . '.zip';
$realFileName = $filename;
$file = "download/".$realFileName;
$fp = fopen($file, 'rb');
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=$fakeFileName");
header("Content-Length: " . filesize($file));
fpassthru($fp);
exit;
}
$stmt->free_result();
$stmt->close();
错误:
<b>Warning</b>: fopen(download/6694488-PHPUploadFile.zip): failed to open stream: No such file or directory in <b>C:\xampp\htdocs\project\modules\download.inc.php</b> on line <b>27</b><br />
<br />
<b>Warning</b>: filesize(): stat failed for download/6694488-PHPUploadFile.zip in <b>C:\xampp\htdocs\project\modules\download.inc.php</b> on line <b>31</b><br />
<br />
<b>Warning</b>: fpassthru() expects parameter 1 to be resource, boolean given in <b>C:\xampp\htdocs\project\modules\download.inc.php</b> on line <b>32</b><br />
答案 0 :(得分:0)
您使用的是相对路径,而fopen()
需要绝对路径。使用任何常量/方法/等,它们为您提供项目目录的绝对路径,以构建文件的绝对路径。
这样做的一种可能性是使用服务器变量:
$file = $_SERVER['DOCUMENT_ROOT'] . '/modules/download/' . $realFileName;