我正在开发一个项目,我获取文件流并将此文件写入服务器本地磁盘。
然后我希望PHP下载它,但它只是将文件的数据转储到页面。
下面是我如何编写文件并试图告诉PHP下载它
$settingsManager = new SettingsManager();
$this->tempWriteLocation = $settingsManager->getSpecificSetting("hddFileWriterLocation");
$downloadUrl = $settingsManager->getSpecificSetting("tempFileUrlDownload") . "/$this->tempFileName";
if (!$this->checkIfDirectoryExists())
{
throw new Exception("Failed to create temp write directory: $this->tempWriteLocation");
}
$filePathAndName = "$this->tempWriteLocation\\$this->tempFileName";
$fh = fopen($filePathAndName, "w");
if (!$fh)
{
throw new Exception("Failed to open file handle for: $filePathAndName. " . error_get_last());
}
fwrite($fh, $this->fileData);
fclose($fh);
//return $downloadUrl;
header('Content-Description: File Transfer');
header('Content-Type: audio/wav');
header('Content-Disposition: attachment; filename='.basename($filePathAndName));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filePathAndName));
ob_clean();
flush();
readfile($filePathAndName);
当上面的代码运行时,我得到以下输出(只是一个片段)
RIFF \ tWAVELIST2INFOISFT%Aculab媒体系统服务器V2.3.4b11fmt @@ factsdatasUUUUUUUUUUUUUUUUUUUUUUUUUUU
只是你知道钻石是我回来的实际输出,没有任何错误的SO显示正确的东西。
我已尝试将内容类型设置为强制下载,但没有任何区别。
感谢您提供的任何帮助。
答案 0 :(得分:1)
试试这个标题:
header('Content-type: audio/x-wav', true);
header('Content-Disposition: attachment;filename=wav-filename.wav');
看看这是否有效。从我看到你有正确的代码形成设置。修复标题应自动下载文件。