use*_*680 5 javascript php session refresh download
最终目标:单击第1页上的链接,最后下载文件并刷新页面1.使用PHP提供不在公共HTML中的下载.
做法:
第1页. 链接传输到第2页,获取我正在使用的文件的get变量引用.
第2页. 使用在刷新页面1之前需要更新的信息更新相关的SQL数据库.设置"firstpass"会话变量.从get变量设置会话变量"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);
}
}
Run Code Online (Sandbox Code Playgroud)
第2页上的脚本工作正常.它更新了sql数据库并正确地重定向到主页面.我还检查过它设置了"$ _SESSION ['passreference'];" 正确,第1页上的任何内容都不会取消它.
所以,这就是对情况的全面解释.我很难过.会发生什么,正如我所说,第2页工作正常.然后它踢到第1页,刷新,然后不推送任何下载.我知道下载脚本可以工作,并且文件可以下载(在没有整个刷新序列的情况下进行检查).
我基本上有两个问题:
谁能发现什么出错?
任何人都可以概念化更好的方法吗?
我刚刚稍微修改了你的 PHP 代码。特别是,您将获得有关问题所在的更多信息。如果您收到新的错误消息之一,只需尝试此代码并阅读以下注释,其中解释了发生的情况。另请阅读下面的“注释”部分,它解释了为什么您可能无法从 PHP 访问文件,即使该文件已存在且位于正确的目录中。
所以这是源代码:
<?php
function error($message, $info = "") {
echo "ERROR: $message<br>";
echo "PRIVATE-INFO: $info"; // probably you only want to log that into a file?
exit;
}
// REFERSH IF FIRSTPASS IS LIVE
if ($_SESSION["PASS1"] == "YES") {
$_SESSION["PASS1"] = "no";
$_SESSION["PASS2"] = "YES";
echo "<script>window.location.reload();</script>";
exit;
}
if ($_SESSION["PASS2"] == "YES") {
// Grab reference data from session:
$id = $_SESSION['passreference'];
if (!$id) error("Internal Error ('id' not set)");
// Select file location from DB
$id = addslashes($id);
$query = "SELECT * from rightplace WHERE id = '$id'";
$result = mysql_query($query);
if (!$result) error("DB-query execution error", mysql_error());
$row = mysql_fetch_array($result);
mysql_free_result($result);
if (!$row) error("File with ID '$id' was not found in DB.");
$filename = $row['file'];
$uploader = $row['uploader'];
// Setting up download variables
$rootpath = "/home/domain/aboveroot";
$filepath = $rootpath . "/" . $uploader . "/" . $filename;
$ext = strtolower(end(explode('.', $filename)));
// Serve the file download
// List of known extensions and their MIME-types...
$typelist = array(
"pdf" => "application/pdf",
"doc" => "application/msword",
"txt" => "text/plain",
"rtf" => "application/rtf",
"docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"ppt" => "application/vnd.ms-powerpoint"
);
// set default content-type
$type = "application/octet-stream";
// for known extensions, assign specific content-type
if (!isset($typelist[$ext])) $type = $typelist[$ext];
if (file_exists($filepath)) {
header("Content-disposition: attachment; filename= '$filename'");
header("Content-type: $type");
readfile($filepath);
} else {
error("Error: File '$filepath' was not found!", $filepath);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
即使文件存在,也可能会发生文件未找到错误。如果发生这种情况,这很可能是一种安全机制,阻止 PHP 脚本访问 HTML 根目录之外的文件。例如,php 脚本可以在“chrooted”环境中执行,其中根目录“/”被映射到“/home/username/”。因此,如果您想访问“/home/username/dir/file”,您需要在 PHP 脚本中写入“/dir/file”。如果你的 root 设置为“/home/username/html”,情况可能会更糟;那么您将无法访问“html”目录下的目录。要解决这个问题,您可以在 HTML 根目录中创建一个目录,并在其中放置一个名为“.htaccess”的文件。在其中写入“DENY FROM ALL”,这将阻止浏览器请求访问该目录(只有脚本可以访问它)。这仅适用于 apache 服务器。但是其他服务器软件也有类似的解决方案...更多信息可以在以下位置找到:http ://www.php.net/manual/en/ini.core.php#ini.open-basedir
另一种可能性是您的文件访问权限(对于上传的文件)没有以允许您的脚本访问它们的方式设置。启用某些安全设置(在 Linux 服务器上)后,您的 PHP 脚本只能访问与为脚本文件设置的“所有者”相同的用户所拥有的文件。通过“ftp”上传后,这很可能是 ftp 用户的用户名。如果在 shell 上进行编辑,这将是当前用户的用户名。=> 但是:上传的文件有时会分配给运行网络服务器的用户(例如“www-data”、“www-run”或“apache”)。因此,找出它是什么并将您的脚本分配给该所有者。
归档时间: |
|
查看次数: |
2085 次 |
最近记录: |