2
我想在會話內進行文件下載。下面是代碼:在會話中提供文件下載
<?php>
session_name("My-Download");
session_start();
$_SESSION['Download-Authorized'] = 1;
echo "<a class='invlink' rel='nofollow' download target='_blank' href='download.php?download_file=file.to.download.pdf'>Name of File</a><br /><br />";
?>
下載腳本( '的download.php')隨之而來的:只要
<?php
session_start();
if(!isset($_SESSION['Download-Authorized'])) {
exit;
}
$path = $_SERVER['DOCUMENT_ROOT']."/downdir/";
$fullPath = $path.$_GET['download_file'];
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);
while(!feof($fd)) {
$buffer = fread($fd, 2048);
print($buffer);
flush();
}
fclose ($fd);
} else {
die("File does not exist. Make sure you specified correct file name.");
}
exit;
?>
一切工作正常,作爲 '$ _SESSION [' 下載授權」驗證] ist評論說。 當我檢查會話變量$ _SESSION ['Download-Authorized']設置爲 時,下載將失敗。
我的代碼有什麼問題?
任何幫助表示讚賞。
將在session_start()到的download.php腳本仍然無法正常工作開始後。 當「download.php」被調用時,會話ID和會話名稱會發生變化。另外$ _SESSION ['Downlad-Autorized']被重置。
'download.php'需要'在session_start()'開頭。 – Barmar
你有沒有使用<?php session_start(); ?> – souvickcse
加入<?php session_start(); ?>在download.php開始處不會改變下載腳本的失敗。 – user3647214