简体   繁体   English

PHP输出缓冲(ob_start,ob_flush)

[英]PHP output buffering (ob_start, ob_flush)

I used php output buffering earlier in order to create csv file from database, because i didn't want to create an existing file, just wanted to make content downloadable. 我以前使用php输出缓冲来从数据库创建csv文件,因为我不想创建现有文件,只是想使内容可下载。

CSV is text-based file, so its easy to create this way, you set the header and flush the text content. CSV是基于文本的文件,因此可以轻松地通过这种方式创建,设置标题并刷新文本内容。 But! 但! What if, I want to create an exe from hex data? 如果我想从十六进制数据创建一个exe怎么办? (The project is: I have an existing exe file and I want that users can write something inside a HTML textbox and I convert that to hex and exchange the old text to the new one) (该项目是:我有一个现有的exe文件,并且我希望用户可以在HTML文本框中编写一些内容,然后将其转换为十六进制并将旧文本替换为新文本)。

Thanks. 谢谢。

Use following code to download exe file by reading from hard disk. 使用以下代码通过从硬盘读取文件来下载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;
}

If you want to replace part of binary file, for example from 100th byte to 200th byte you could use substr() and str_pad(): 如果要替换二进制文件的一部分,例如从第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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM