Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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 - Fatal编程技术网

返回页面后继续运行php脚本

返回页面后继续运行php脚本,php,Php,我正在尝试编写一个脚本,在服务器上创建一个文件,然后使用header()将用户重定向到该文件。然后,大约10秒钟后,我想删除该文件。我试过这个: header('Location: '.$url); flush(); sleep(10); unlink($url); 但浏览器只是等待脚本完成,然后被重定向,但此时文件已被删除。有没有办法告诉浏览器“文件结束”,然后继续计算?或者让PHP启动另一个脚本,但不要等待该脚本完成?您可以尝试这样做: <iframe src="<?=$url

我正在尝试编写一个脚本,在服务器上创建一个文件,然后使用
header()
将用户重定向到该文件。然后,大约10秒钟后,我想删除该文件。我试过这个:

header('Location: '.$url);
flush();
sleep(10);
unlink($url);

但浏览器只是等待脚本完成,然后被重定向,但此时文件已被删除。有没有办法告诉浏览器“文件结束”,然后继续计算?或者让PHP启动另一个脚本,但不要等待该脚本完成?

您可以尝试这样做:

<iframe src="<?=$url?>"></iframe>

....
<?
sleep(10);
unlink($url);
?>

到目前为止,我发现实现这一点的唯一方法是在标题中提供内容长度。尝试添加以下内容:

header("Content-Length: 0");
在你脸红之前()


使用时要非常小心,您可以很快地通过滥用服务器杀死它。

您最好让PHP页面为文件提供服务。在这种情况下,不需要创建临时文件并将其删除,只需将要写入临时文件的数据发送出去即可。您需要正确设置标题,以便浏览器能够识别您发送的文件类型。i、 e.内容类型:文本/xml;对于xml或jpg的图像/jpeg


此方法还处理下载文件所需时间较长的慢速客户端

或者。。。。与动态生成文件相比。。。为什么不这样做呢

php?key={md5 hash}


然后,tempFile.php查询DB、memcache(带有附加的预加键)或apc中的内容。

这是错误的。您可以创建文件并提供给他们,然后一步删除它

<?php
$file_contents = 'these are the contents of your file';
$random_filename = md5(time()+rand(0,10000)).'.txt';
$public_directory = '/www';
$the_file = $public_directory.'/'.$random_filename;
file_put_contents($the_file, $file_contents);
echo file_get_contents($the_file);
unlink($the_file);
?>

如果这样做,用户看到文件后会立即将其删除。当然,这意味着文件首先不需要存在。因此,您可以将代码缩短为:

<?php
$file_contents = 'these are the contents of your file';
echo $file_contents;
?>

这完全取决于你从哪里得到你想展示给他们的内容。如果它来自文件,请尝试:

<?php
$file_contents = file_get_contents($filename_or_url);
echo $file_contents;
?>


至于自动删除文件,只需设置一个cron作业,该作业每10秒运行一次,并删除临时文件夹中filemtime($filename)大于5分钟秒的所有文件。

如果您想实现您的原始设计,请阅读关于在PHP中运行“fire and forget”命令的问题

\Symfony\Component\HttpFoundation\Response::send

/**
 * Sends HTTP headers and content.
 *
 * @return Response
 *
 * @api
 */
public function send()
{
    $this->sendHeaders();
    $this->sendContent();

    if (function_exists('fastcgi_finish_request')) {
        fastcgi_finish_request();
    } elseif ('cli' !== PHP_SAPI) {
        // ob_get_level() never returns 0 on some Windows configurations, so if
        // the level is the same two times in a row, the loop should be stopped.
        $previous = null;
        $obStatus = ob_get_status(1);
        while (($level = ob_get_level()) > 0 && $level !== $previous) {
            $previous = $level;
            if ($obStatus[$level - 1]) {
                if (version_compare(PHP_VERSION, '5.4', '>=')) {
                    if (isset($obStatus[$level - 1]['flags']) && ($obStatus[$level - 1]['flags'] & PHP_OUTPUT_HANDLER_REMOVABLE)) {
                        ob_end_flush();
                    }
                } else {
                    if (isset($obStatus[$level - 1]['del']) && $obStatus[$level - 1]['del']) {
                        ob_end_flush();
                    }
                }
            }
        }
        flush();
    }

    return $this;
}

是的,创建一个页面然后销毁它是一种资源浪费,只需将此文件打印给用户,一举两得。如果您试图执行诸如输出CSV之类的操作,那么将其打印到浏览器仍然是最好的方法。