如何下载解密文件而不先写入磁盘

时间:2013-01-14 20:23:09

标签: php

我有一个脚本,可以在磁盘上为给定的$path变量(即:/var/recordings/file.mp3)获取mp3文件,并将其发送到浏览器,由媒体播放器播放。这是代码:

//Send the media asset to the browser
header('Content-type: audio/mpeg');
header('Content-length: ' . filesize($path));
header('Content-Disposition: filename="recording.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
header("Content-Transfer-Encoding: binary"); 
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
print file_get_contents($path);

我现在以加密形式存储有问题的mp3文件,所以我现在将有问题的文件读成字符串并使用加密类对其进行解密。我现在想将这个解密文件直接发送到浏览器而不将其输出到文件中,然后让PHP读取该文件的内容。可能吗?我想我可以做print $decrypted_string,但我如何指定文件大小?

1 个答案:

答案 0 :(得分:2)

首先解密它。然后计算strlen函数的大小。将该值用作Content-Length标头值。

$contents = file_get_contents($path);
$decrypted_string = decrypt_mp3($contents);
header('Content-length: ' . strlen($decrypted_string));
echo $decrypted_string;
相关问题