2012-11-02 80 views
0

我正在开发可下载内容的安全性,以简化我的下载内容。如何在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个字节(我的自定义信息有关下载文件按每次下载)

我想一些方法来添加这些100个字节的ReadFile后($文件);所以下载的文件将会有这个额外的信息。

something like 
readfile($file) + myBytes() 

我是新来的PHP,请帮我

+1

这取决于您下载的文件的类型。简单地将额外的字节粘贴到文件的末尾可能会导致其被损坏。 –

+0

也许你应该考虑使用安全密钥的加密方法呢? http://stackoverflow.com/questions/2448256/php-mcrypt-encrypting-decrypting-file –

回答

2

存储在一个文件中,变量的额外的字节,DB或等,并呼应它读取后文件功能

$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; 
} 
+0

就这么简单 - 感谢您节省我的时间 现在我开发了我的解决方案,我的.net可下载应用程序使用这些附加信息进行许可证验证 –

+0

您需要将额外字节的长度添加到Content-Length标题也是如此。 –

0

如果你只处理图像,我推荐使用steganography而不是只粘贴字节到文件的末尾。谷歌搜索“PHP的隐写库”变成了几命中:

+0

我已经在Matlab中实现了隐写,但这只是一个来自php.net的示例代码,实际上我的内容是使用这个添加的执行文件许可信息。 –

相关问题