我正在尝试在Yii中编写一个脚本,用于从服务器下载文件。
这些文件位于Yii项目的webroot中,
但是每次文件都不存在错误,有人可以看到哪里出错:
public function actionDownload($id) {
$audio = Audio::model()->findByPk($id);
$file = Yii::getPathOfAlias('webroot') . $audio->path;
if (file_exists($file)) {
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $audio->path);
header('Content-Length: ' . filesize($audio->path));
$audio->downloaded = $audio->downloaded + 1;
$audio->save();
}else{
echo "file not exist: ".$file;
}
exit;
}
我得到的错误是:
file not exist: /var/www/vhosts/ikhwanbiz.org/httpdocs/userfiles/reklames/media/deneme/sen%20dep%20olmisem.mp3
由于
此致
壁立
答案 0 :(得分:3)
Bili,这对我来说效果很好,在大多数浏览器上看起来都很好。
$filename = 'your_file_name.csv';
header('Content-Disposition: attachment; charset=UTF-8; filename="'.$filename.'"');
$utf8_content = mb_convert_encoding($content, "SJIS", "UTF-8");
echo $utf8_content;
Yii::app()->end();
return;
希望它有所帮助,祝你好运!
答案 1 :(得分:0)
看起来文件名部分$audio->path
是URL编码的,而服务器上的实际文件名不是。你应该在源头修改它(不知道从哪里设置路径),但同时一个简单的解决方法是编写
$file = Yii::getPathOfAlias('webroot') . urldecode($audio->path);
答案 2 :(得分:0)
这更像是一个php问题,而不是一个问题。
例如,
<?php
header("Content-disposition: attachment; filename=huge_document.pdf");
header("Content-type: application/pdf");
readfile("huge_document.pdf");
?>