我想知道如何获取对象的值。我正在使用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'。
答案 0 :(得分:1)
尝试
<?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())); ?>
的文档
答案 1 :(得分:0)
我重写了我的代码,如下所示:
我的模特:
<?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()));
}?>