【发布时间】:2014-01-08 04:56:45
【问题描述】:
我已经关注yii网站上传图片,代码在这里:
class ItemController extends CController
{
public function actionCreate()
{
$model=new Item;
if(isset($_POST['Item']))
{
$model->attributes=$_POST['Item'];
$model->image=CUploadedFile::getInstance($model,'image');
if($model->save())
{
$model->image->saveAs('path/to/localFile');
// redirect to success page
}
}
$this->render('create', array('model'=>$model));
}
}
但是如何通过 currentdate+filename.png 重命名文件并上传到路径,我还需要更新和删除代码。 非常感谢你
我已经解决了这个问题:
public function currentDate(){
$date = date('m-d-Y-h-i-s', time());
return $date;
}
public function actionCreate(){
$model = new News();
if(isset($_POST['News']))
{
$model->attributes=$_POST['News'];
$uploadedFile = CUploadedFile::getInstance($model, 'images');
$fileName = "{$this->currentDate()}-{$uploadedFile}";
$model->images = $fileName;
if($model->save()){
$uploadedFile->saveAs("upload/".$fileName);
$this->redirect(array('news/index'));
}else{
$model = new News();
$this->render('create',
array('model' =>$model,
'result'=>'insert new fail !',
));
}
}else{
$this->render('create',
array(
'model'=>$model,
));
}
}
【问题讨论】:
-
检查这个链接:stackoverflow.com/questions/20359685/…问题本身你有答案。
-
我的问题是我们上传时如何重命名文件,例如文件命名为test.png,当我上传到文件夹时,我希望文件夹中的文件命名为currentdate-test.png,谢谢你的回答
-
现在我有自己的解决方案来解决这个问题。
标签: yii