yii 如何打印出对象的值

3lxsmp7m  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(207)

我想知道如何获取对象的值。我正在使用Yii框架来实现一个下载功能。How to pass a parameter from frontend
我打印出了一个对象,但我不知道如何从该对象中获取值。

Array
(
    [file] => CUploadedFile Object
        (
            [_name:CUploadedFile:private] => 23602414.pdf
            [_tempName:CUploadedFile:private] => D:\wamp\tmp\php8780.tmp
            [_type:CUploadedFile:private] => application/pdf
            [_size:CUploadedFile:private] => 181004
            [_error:CUploadedFile:private] => 0
            [_e:CComponent:private] => 
            [_m:CComponent:private] => 
        )

    [layout] => //layouts/column1
    [menu] => Array
        (
        )

    [breadcrumbs] => Array
        (
        )

    [defaultAction] => index
    [_widgetStack] => Array
        (
        )

)

我想获取“23602414.pdf”,并将其存储到变量中。
这是我的密码。

<?php $model=new Upload(); ?>

<?php if(isset($_POST['Upload'])){$model->attributes=$_POST['Upload'];

$this->file=CUploadedFile::getInstance($model,'file');

}?>

<?php echo CHtml::link('Download file',array('/upload/download','id'=>'23602414.pdf')); ?>

我不想硬编码为'id'=〉' 23602414.pdf ',我想在那里回显文件名。

ut6juiuv

ut6juiuv1#

试试这个

<?php $model=new Upload(); ?>

<?php if(isset($_POST['Upload'])){$model->attributes=$_POST['Upload'];

$this->file=CUploadedFile::getInstance($model,'file');

}?>

<?php echo CHtml::link('Download file',array('/upload/download','id'=>$this->file->getName())); ?>

此处为文档https://www.yiiframework.com/doc/api/1.1/CUploadedFile

7uhlpewt

7uhlpewt2#

我重写了我的代码如下:
我的模型:

<?php

class Upload extends CFormModel
{
    public $file;
    public function rules()
    {
        return array(
                array('file', 'file', 'types'=>'pdf'),
        );
    }

    public function downloadFile($fullpath){
        $dir = Yii::getPathOfAlias('application.uploads');
        $filename= $fullpath;

        if(!empty($fullpath)){
            $file = $dir."\\"."$filename";

            header("Content-type: application/pdf");
            header("Content-Disposition: inline; filename=$filename");
            @readfile($file);

            Yii::app()->end();
        }
        else {return false;}

    }
}

我的控制器:

<?php

class UploadController extends Controller
{
     public  $file;

    function actionIndex()
    {
        $dir = Yii::getPathOfAlias('application.uploads');

        $uploaded = false;
        $model=new Upload();

        if(isset($_POST['Upload']))
        {
            $model->attributes=$_POST['Upload'];

            $this->file=CUploadedFile::getInstance($model,'file');

            $file=$this->file;

            if($model->validate()){
                $uploaded = $file->saveAs($dir.'/'.$file->getName());

            }
        }
        $this->render('index', array(
                'model' => $model,
                'uploaded' => $uploaded,
                'dir' => $dir,
        ));
    }

    public function actionDownload($id){

        $path = Yii::getPathOfAlias('/yiiroot/trackstar/protected/uploads/')."$id";

        $upload=new Upload();

        $upload->downloadFile($path);
    }
}

我的看法:

<?php if($uploaded):?>

<p>File was uploaded. Check <?php echo $dir?>.</p>

<?php endif ?>
<?php echo CHtml::beginForm('','post',array
('enctype'=>'multipart/form-data'))?>
<?php echo CHtml::error($model, 'file')?>
<?php echo CHtml::activeFileField($model, 'file')?>
<?php echo CHtml::submitButton('Upload')?>
<?php echo CHtml::endForm()?>
<br/>

<?php $model=new Upload(); ?>

<?php if(isset($_POST['Upload'])){$model->attributes=$_POST['Upload'];

$this->file=CUploadedFile::getInstance($model,'file');

echo CHtml::link('Download file',array('/upload/download','id'=>$this->file->getName())); 

}?>

请参阅:PHP getName() Function

相关问题