使用PHP ob_start&;输出流;把你弄干净
我有一个脚本,使用PHP ob_start&;输出流;把你弄干净,php,ob-start,ob-get-contents,Php,Ob Start,Ob Get Contents,我有一个脚本,echo在php脚本中输出内容,并生成一个非常大的文件,例如100MB 目前,我使用以下方法捕获输出并写入另一个文件 ob_start(); require_once 'dynamic_data.php'; // echo 100MB data $data = ob_get_clean(); file_put_contents($path, $data); 有没有简单的方法可以重新编写上面的程序(最好不要接触动态数据.php,因为很难重新计算),这样它就可以直接将输出流式传输到文
echo
在php脚本中输出内容,并生成一个非常大的文件,例如100MB
目前,我使用以下方法捕获输出并写入另一个文件
ob_start();
require_once 'dynamic_data.php'; // echo 100MB data
$data = ob_get_clean();
file_put_contents($path, $data);
有没有简单的方法可以重新编写上面的程序(最好不要接触动态数据.php,因为很难重新计算),这样它就可以直接将输出流式传输到文件,而不必将内容保存在内存中?您可以使用此文件作为参数来调用php解释器。这不会将数据存储在内存中,但会创建另一个进程
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("file", $path, "w"), // stdout is a pipe that the child will write to
2 => array("file", $path, "a") // stderr is a file to write to
);
$process = proc_open('php dynamic_data.php', $descriptorspec, $pipes);
if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be appended to /tmp/error-output.txt
fclose($pipes[0]);
fclose($pipes[1]);
$return_value = proc_close($process);
}
?>
提供了一种解决方法。您需要传入一个$output\u回调
和一个$chunk\u大小
假设您将$chunk\u size
设置为1MB。然后,每隔1MB的缓冲输出数据,您的$output\u回调将被调用此数据,您可以将其刷新到磁盘(同时隐式刷新输出缓冲区)
您不能直接在dymanic_data.php
中添加文件操作,而不回显并重新捕获输出吗?这样做有什么限制吗?因为使用PHP作为模板语言比直接打印到文件更容易循环/回显。此外,我还可以将相同的脚本重新用于web输出。
$output_callback = function($data) {
//$buffer contains our 1MB of output
file_put_contents($path, $data);
//return new string buffer
return "";
}
//call $output_callback every 1MB of output buffered.
ob_start($output_callback, 1048576);
require_once 'dynamic_data.php';
//call ob_clean at the end to get any remaining bytes
//(implicitly calls $output_callback final time)
ob_clean();