如何在PHP下载内容结束时添加额外的数据

时间:2012-11-02 17:25:12

标签: php

我正在开发可下载内容的安全性,只是保护我的下载内容。

<?php
$file = 'monkey.gif';

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;
}
?>

现在我只想添加100个额外字节(根据每次下载我的下载文件的自定义详细信息)

我想要一些方法在readfile($ file)之后添加这100个字节;所以下载的文件中会有这些额外的信息。

something like 
readfile($file) + myBytes()

我是PHP新手所以请帮帮我

2 个答案:

答案 0 :(得分:2)

将额外的字节存储在文件,变量,数据库等中,并在读取文件函数

后回显它
$file = 'monkey.gif';

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);
    readfile("extra_filename.ext");
    /* 
    //or use this
    $extra_bytes = "store extra bytes here";
    echo $extra_bytes;
    */
    exit;
}

答案 1 :(得分:0)

如果您只处理图像,我建议使用steganography而不是仅将字节粘贴到文件末尾。谷歌搜索“PHP隐写术图书馆”出现了一些点击: