PHP输出缓冲(ob_start,ob_flush)
问题描述:
我之前使用php输出缓冲来创建数据库中的csv文件,因为我不想创建现有文件,只是想让内容可下载。PHP输出缓冲(ob_start,ob_flush)
CSV是基于文本的文件,因此很容易以这种方式创建,您可以设置标题并清除文本内容。但!如果我想从十六进制数据创建一个exe文件呢? (该项目是:我有一个现有的exe文件,我希望用户可以在HTML文本框中写入内容,并将其转换为十六进制并将旧文本交换到新文件)
谢谢。
答
如果你想从100字节你可以使用substr()和str_pad()第200字节替换二进制文件的一部分,例如:
$binFile = file_get_contents('/pathto/exec/file.exec');
$replacedBinFile = substr($binFile, 0, 100) . str_pad(substr($_POST['text'], 0, 100), 100, "\x00") . substr($binFile, 200);
file_put_contents('/pathto/exec/file_replaced.exec', $replacedBinFile);
答
使用下面的代码从硬盘读取下载exe文件。
<?php
$file = 'test.exe';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
你知道在什么范围内的字节你想覆盖的exe文件?我做的是 – piotrekkr
。它总共约100-200字节 – Iburidu