我之前使用php输出缓冲来从数据库创建csv文件,因为我不想创建现有文件,只是想让内容可下载。
CSV是基于文本的文件,因此很容易以这种方式创建,您可以设置标题并刷新文本内容。但!如果,我想从十六进制数据创建一个exe怎么办? (项目是:我有一个现有的exe文件,我希望用户可以在HTML文本框中写一些内容,然后将其转换为十六进制并将旧文本交换为新文本)
感谢。
答案 0 :(得分:3)
使用以下代码通过从硬盘读取来下载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;
}
答案 1 :(得分:1)
如果要替换部分二进制文件,例如从第100字节到第200字节,可以使用substr()和str_pad():
$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);