【发布时间】:2012-08-04 18:27:55
【问题描述】:
最终目标: 点击第 1 页的链接,最终下载文件并刷新第 1 页。使用 PHP 提供不在公共 html 中的下载。
方法:
第 1 页。 链接转移到第 2 页,获取我正在使用的文件的变量引用。
第 2 页。 使用刷新页面 1 之前需要更新的信息更新相关 SQL 数据库。设置“firstpass”会话变量。从获取变量中设置会话变量“getvariablereference”。重定向到第 1 页。
第 1 页。 如果首先通过会话变量集。设置第二遍会话变量。取消设置第一遍变量。刷新页面。重新加载时,页面将使用更新的 SQL 数据库信息重建(在第 2 页更改。)。
已刷新第 1 页。 如果设置了第二遍会话变量。运行下载服务标头序列。
这是第 1 页。我没有显示第 1 页中具有初始链接的部分。既然没关系。
// REFERSH IF FIRSTPASS IS LIVE
if ($_SESSION["PASS1"] == "YES"){
$_SESSION["PASS1"] = "no";
$_SESSION["PASS2"] = "YES";
echo "<script>document.location.reload();</script>";
}
if ($_SESSION["PASS2"] == "YES"){
// Grab reference data from session:
$id = $_SESSION['passreference'];
// Serve the file download
//First find the file location
$query = "SELECT * from rightplace
WHERE id = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$filename = $row['file'];
$uploader = $row['uploader'];
// Setting up download variables
$string1 = "/home/domain/aboveroot/";
$string2 = $uploader;
$string3 = '/';
$string4 = $filename;
$file= $string1.$string2.$string3.$string4;
$ext = strtolower (end(explode('.', $filename)));
//Finding MIME type
if($ext == "pdf" && file_exists($file)) {
header("Content-disposition: attachment; filename= '$filename'");
header('Content-type: application/pdf');
readfile($file);
}
if($ext == "doc" && file_exists($file)) {
header("Content-disposition: attachment; filename= '$filename'");
header('Content-type: application/msword');
readfile($file);
}
if($ext == "txt" && file_exists($file)) {
header("Content-disposition: attachment; filename= '$filename'");
header('Content-type: text/plain');
readfile($file);
}
if($ext == "rtf" && file_exists($file)) {
header("Content-disposition: attachment; filename= '$filename'");
header('Content-type: application/rtf');
readfile($file);
}
if($ext == "docx" && file_exists($file)) {
header("Content-disposition: attachment; filename= '$filename'");
header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
readfile($file);
}
if($ext == "pptx" && file_exists($file)) {
header("Content-disposition: attachment; filename= '$filename'");
header('Content-type: application/vnd.openxmlformats-officedocument.presentationml.presentation');
readfile($file);
}
if($ext == "ppt" && file_exists($file)) {
header("Content-disposition: attachment; filename= '$filename'");
header('Content-type: application/vnd.ms-powerpoint');
readfile($file);
}
}
第 2 页上的脚本工作正常。它会更新 sql 数据库并正确重定向到主页。我还检查了它是否设置了“$_SESSION['passreference'];”正确,第 1 页上的任何内容都不会取消它。
所以,这就是对这种情况的完整解释。我难住了。正如我所说,第 2 页工作正常。然后它踢到第 1 页,刷新然后不推送任何下载。我知道下载脚本可以正常工作并且可以下载文件(在没有整个刷新序列的情况下进行检查)。
我基本上有两个问题:
谁能发现哪里出了问题?
谁能构思出更好的方法?
【问题讨论】:
-
开始学习子程序,在 PHP 中这些被称为函数。这应该可以帮助您更好地解决这个(以及许多其他)编程问题。它们也有助于调试,因此您应该能够更好地发现问题所在。
-
用户点击
Cancel有关系吗? -
难道不是:
window.location.reload()而不是document.location.reload()吗?
标签: php javascript session download refresh