How to print out a value of object

2019-07-24 17:07发布

I want to know how to get a value of object. I am using Yii framework for imeplement a download function. How to pass a parameter from frontend

I printed out a object, but I don't know how to get a value from this object.

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
        (
        )

)

I want to get the "23602414.pdf", and store it to a varable.

This is my code.

<?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')); ?>

Instead of hard code as 'id'=>'23602414.pdf', I want to echo the file name in there.

标签: php yii
2条回答
\"骚年 ilove
2楼-- · 2019-07-24 17:16

try this

<?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())); ?>

Documentation here https://www.yiiframework.com/doc/api/1.1/CUploadedFile

查看更多
做个烂人
3楼-- · 2019-07-24 17:30

I rewrited my code like below:

My model:

<?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;}

    }
}

My controller:

<?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);
    }
}

My view:

<?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())); 

}?>

refer:PHP getName() Function

查看更多
登录 后发表回答