在 Yii 中下载文件 [英] Download File in Yii

查看:37
本文介绍了在 Yii 中下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Yii 中编写一个脚本来从服务器下载文件.

I am trying to write a script in Yii for downloading files from the server.

文件位于 Yii 项目的 webroot 中,

The files are located in webroot of the Yii project,

但是我每次都遇到文件不存在的错误,谁能看出哪里错了:

but I got every time file not exist error, could anyone see where is wrong:

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

谢谢

问候

比利

推荐答案

Bili,这对我来说效果很好,而且在大多数浏览器上似乎都很好.

Bili, this works well for me and seem to be fine on most browsers.

$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;

希望能帮到你,祝你好运!

Hope it helps, good luck!

这篇关于在 Yii 中下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆