Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 下载脚本中的刷新无效_Php_Download_Refresh - Fatal编程技术网

Php 下载脚本中的刷新无效

Php 下载脚本中的刷新无效,php,download,refresh,Php,Download,Refresh,我有这个下载脚本,它工作得很好,但一旦文件被下载,我希望页面重新加载。但是一旦文件被下载,页面就消失了?它应该是一个安全的下载脚本,这样用户就看不到文件的路径。它是通过使用POST提交来调用的 <?php session_start(); //Assign variables from session $userid = $_SESSION["id"]; $email = $_SESSION['email']; $password = $_SESSION['password']; //

我有这个下载脚本,它工作得很好,但一旦文件被下载,我希望页面重新加载。但是一旦文件被下载,页面就消失了?它应该是一个安全的下载脚本,这样用户就看不到文件的路径。它是通过使用POST提交来调用的

<?php
session_start();

//Assign variables from session
$userid = $_SESSION["id"];
$email = $_SESSION['email'];
$password = $_SESSION['password'];

//Require database connection and functions
require('includes/config.php');
include('includes/functions.php');

//Check if user is logged in
if (!$userid || !$email || !$password)
{
    header("Location: index.php");
}       

// Usage: <a href="download.php?file=test.txt&category=test">Download</a> 
// Path to downloadable files (will not be revealed to users so they will never know your file's real address) 
$hiddenPath = "uploaded_notes/";

// VARIABLES 
if (!empty($_POST['file'])){
$file_id = $_POST['file'];
    $result = mysql_query("SELECT * FROM files WHERE id='$file_id'");
    $row = mysql_fetch_array($result);
$file = $row['location'];
$price = $row['price'];

    $result2 = mysql_query("SELECT coins FROM users WHERE id='$userid'");
    $row2 = mysql_fetch_array($result2);
$coins = $row2['coins'];

$new_coins = $coins-$price;
if ($new_coins >= 0)
{
    mysql_query("UPDATE users SET coins = $new_coins WHERE id='$id'");
}
else {
header("Location: dashboard.php");
die();
}

//Insert into purchases
$datetime = date("Y-m-d H:i:s");@
mysql_query("INSERT INTO purchases (userid, fileid, datetime) VALUES ('$userid', '$file_id', '$datetime')");

$file = str_replace('%20', ' ', $file); 
$category = (!empty($_GET['category'])) ? $_GET['category'] . '/' : ''; 
} else {
    header('Location: dashboard.php');
    die('Hacking attempt reported.');
}

$file_real = $hiddenPath . $category . $file; 
$ip = $_SERVER['REMOTE_ADDR'];

// If requested file exists 
if (file_exists($file_real)){ 
// Get extension of requested file 
$extension = strtolower(substr(strrchr($file, "."), 1)); 
// Determine correct MIME type 
switch($extension){ 
case "asf": $type = "video/x-ms-asf"; break; 
case "avi": $type = "video/x-msvideo"; break; 
case "exe": $type = "application/octet-stream"; break; 
case "mov": $type = "video/quicktime"; break; 
case "mp3": $type = "audio/mpeg"; break; 
case "mpg": $type = "video/mpeg"; break; 
case "mpeg": $type = "video/mpeg"; break; 
case "rar": $type = "encoding/x-compress"; break; 
case "txt": $type = "text/plain"; break; 
case "wav": $type = "audio/wav"; break; 
case "wma": $type = "audio/x-ms-wma"; break; 
case "wmv": $type = "video/x-ms-wmv"; break; 
case "zip": $type = "application/x-zip-compressed"; break; 
case "jpg": $type = "image/jpeg"; break; 
default: $type = "application/force-download"; break; 
} 
// Fix IE bug [0] 
$header_file = (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) ? preg_replace('/\./', '%2e', $file, substr_count($file, '.') - 1) : $file; 
// Prepare headers 
header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: public", false); 
header("Content-Description: File Transfer"); 
header("Content-Type: " . $type); 
header("Accept-Ranges: bytes"); 
header("Content-Disposition: attachment; filename=\"" . $header_file . "\";"); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: " . filesize($file_real));



// Send file for download 
if ($stream = fopen($file_real, 'rb')){
while(!feof($stream) && connection_status() == 0){

//reset time limit for big files 
print(fread($stream,1024*8)); 
flush(); 

} 
fclose($stream); 
} 
}else{ 
// Requested file does not exist (File not found) 
echo("Requested file does not exist"); 
die(); 

} 
?>

但在那之后,它就不起作用了……

下载文件是一次性的事情。下载文件后,您将无法重定向。您可以尝试在输出内容之前添加刷新标题,但某些浏览器会将其视为取消

有关更多信息,请参阅本文:

您可以阅读,并了解更多信息

基本上,它们都归结为HTTP头。在您回显
内容后,无法发送重定向标题。另一方面,在响应内容之前的重定向头只会取消下载


另一种方法是使用一个隐藏的
iframe
,然后在那里提交请求,但我猜这不是您想要的。

您的“刷新”代码在哪里?它只是标题刷新
print(fread($stream,1024*8));