使用Yii::app()下载->;请求->;SendFile返回损坏的文件


Download using Yii::app()->request->SendFile returns corrupted file

我一直在尝试从如下特定目录下载我上传的文件:

Yii::app()->request->SendFile($model->statement, 
                        file_get_contents($path.$model->statement)
                        ); 

它可以识别文件并以正确的名称和格式下载,但我得到的不是74KB的文件,而是一个1KB的损坏文件。尝试了其他文档,如.doc、.pdf,仍然相同。

我不完全确定file_get_contents到底做了什么,因为我从我发现的一个例子中得到了这个。

要下载的文件以前是使用上传的

<div class="row">
    <?php echo $form->labelEx($model, 'statement'); ?>
    <?php echo $form->fileField($model, 'statement', array('size' => 36, 'maxlength' => 255)); ?>
    <?php echo $form->error($model, 'statement'); ?>
</div>

在_form.php中,并调整控制器中的actionCreate:

public function actionCreate($id)
{
    $model=new Witness;
    $model->ip_id = $id;
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    $path = Yii::app()->basePath . '/modules/iris/statements';
    if (!is_dir($path)) {
        mkdir($path);
    }   
    if(isset($_POST['Witness']))
    {
        $model->attributes=$_POST['Witness'];
        if (@!empty($_FILES['Witness']['name']['statement'])) {
            $model->statement = $_POST['Witness']['statement'];
            if ($model->validate(array('statement'))) {
                $model->statement = CUploadedFile::getInstance($model, 'statement');
            } else {
                $model->statement = '';
            }
            $model->statement->saveAs($path . '/' . time() . '_' . str_replace(' ', '_', strtolower($model->statement)));
            //$model->doc_type = $model->statement->getType();
            //$model->doc_size = $model->statement->getSize();
        }
        $model->statement = time() . '_' . str_replace(' ', '_', strtolower($model->statement));

        if($model->save())
            $this->redirect(array('ip/view','id'=>$model->ip_id));
    }
    $this->render('create',array(
        'model'=>$model,
    ));
}

上传的文件可以毫无问题地打开。知道为什么下载被破坏了吗?在下载之前,我必须先指定文件的类型/大小吗?

对于一个1kb的文件,在设置了头之后,服务器似乎抛出了一个php错误。

使用文本编辑器打开下载的文件并检查内容。

我找到了另一个解决方案,而不是使用SendFile函数。把这个放在视图文件中生成下载链接:

CHtml::link('Click to download file', array('ip/download&id='.$model->file_id));

其中"ip"是模型名称。这将把file_id传递给在controller.php中创建的函数actionDownload:

public function downloadFile($fullpath){
    if(!empty($fullpath)){
        header("Content-type:application/pdf"); //for pdf file
        //header('Content-Type:text/plain; charset=ISO-8859-15');
        //if you want to read text file using text/plain header
        header('Content-Disposition: attachment; filename="'.basename($fullpath).'"');
        header('Content-Length: ' . filesize($fullpath));
        readfile($fullpath);
        Yii::app()->end();
    }
}
public function actionDownload($id){
    $model=Ip::model()->findByPk($id);
    $path = Yii::app()->basePath . 'location of file to be downloaded'.$model->filename;
    $this->downloadFile($path);
}

这应该完全下载文件。不要担心内容类型,我已经尝试了所有常见的文件类型(.pdf、.doc、.ppt、.png、.jpg),所有这些都非常完美。

在Yii2中,sendFile()签名是

public $this sendFile ( $filePath, $attachmentName = null, $options = [] )

发送文件:

Yii::$app->response->sendFile('test.zip');

对于那些,谁得到完整的文件,但仍然损坏:

您应该检查其他php文件是否正在打印输出,有时关闭php标记后的空白可能会导致问题。

从这里

如果查看sendFile的参数列表,可以看到第三个参数是mimeType。事实上,您没有提供HTTP标头响应,所以我的初步建议是尝试明确指定Content-Type响应标头:

Yii::app()->request->sendFile(
   "Youfile.pdf",
   file_get_contents('path/to/file'), 
   'application/pdf'
);