Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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_Apache_Header - Fatal编程技术网

远程服务器PHP中的头文件读取

远程服务器PHP中的头文件读取,php,apache,header,Php,Apache,Header,我的网站正在使用以下代码下载一个文件: $flv = **filename** $ch = curl_init($flv); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not nece

我的网站正在使用以下代码下载一个文件:

$flv = **filename**

$ch = curl_init($flv);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
$data = curl_exec($ch);

curl_close($ch);
if ($data === false) {
  echo 'cURL failed';
  exit;
}

$contentLength = 'unknown';
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
  $contentLength = (int)$matches[1];
}

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($flv));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $contentLength );
ob_clean();
flush();
readfile($flv);
代码可以正常工作,但是,在你完成下载之前,页面上的链接会被阻塞,等待下载完成


我能做什么?

您如何链接到此页面。当然,下载将阻止页面上的所有其他内容,因为它将弹出一个模式窗口,允许用户选择下载文件的位置或取消下载。这是一个浏览器功能

如果你在谈论其他事情,请解释


编辑:

你指的是哪种下载?这里有两个下载。 一种是使用CURL从服务器到服务器下载文件。 另一个是HTTP客户端从服务器下载相同的文件。两者都可以“修复”,但必须具体

如果您希望CURL下载是自动的,那么您必须这样做:下载是一个单独的过程。不幸的是,PHP通常只支持一个进程,所以您必须使用非标准PHP

可能性:

1) 使用外壳

exec('php filename.php downloadfile.ext&')

在这里,您向PHP脚本filename.PHP发送一个参数download.ext。整个过程将在后台执行,因为在命令的末尾有&。 进行var_转储($argv);以查看通过的参数

也可以使用流程管道执行相同的操作:

例如:poopen()proc_open()

他们给你更多的控制权

2) 您还可以安装一个进程控制扩展。

这将允许您从PHP脚本中派生一个单独的进程,该进程将处理文件下载到服务器的过程。还有进程间通信(IPC)扩展,允许您跟踪单独进程中发生的事情

3) 你可以写一个执事来处理下载。基本上,您还需要在deamon中处理多个进程,以便一次进行多个下载。您还需要一点IPC或至少某种形式的并发性,这样您就不会多次下载同一资源。这有点复杂,所以我认为1和2是更好的选择


基本上,您要做的是阻止用户在这里等待服务器到服务器的下载

因此,从主PHP进程(普通PHP页面)开始。 然后你不知何故把这个过程岔开。 让该子进程使用CURL下载文件。 同时,父PHP进程立即返回HTTP客户端。 当子进程完成donwload后,父进程可以通过IPC或轮询并发资源(如下载文件的文件名(或文件大小或锁))来知道。 然后,它将此服务提供给HTTP客户端


如果您的问题是HTTP客户端,那么您必须考虑浏览器解决方案。考虑到您正在测试的确切浏览器,等等,都会让您了解问题所在。

尝试:

$ContentLength = filesize($flv);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($flv));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $ContentLength);

$fh = fopen($flv, 'r');
echo fread($fh, $ContentLength);
fclose($fh);

你在试着移除吗?ob_clean();和flush();对不起,错了,请先尝试不使用标题,看看它在屏幕上的显示效果,然后使用较小的文件Andres,我尝试了,但没有结果。我检查了apache配置,连接也没有任何限制。所以现在我迷路了。这将下载文件两次,这是浪费时间。我找到了一个解决方案,如果我们在下载之前关闭会话_write_(),我就不会等待下载完成,所以问题解决了。谢谢大家!下载文件两次?你是不是在找一个狙击手?