在zend中重命名文件后上传文件 [英] File upload after rename the file in zend
本文介绍了在zend中重命名文件后上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的表单
$ p $
class Admin_Form_RoomtypeForm extends Zend_Form
{
public function init b $ b {
$ name = new Zend_Form_Element_Text('name');
$ name-> setLabel('Name:');
$ imagePath ='/ home / dinuka / image';
$ image = new Zend_Form_Element_File('image');
$ image-> setLabel('Image URL:');
$ image-> setDestination($ imagePath);
$ image-> addValidators(array(array('IsImage',false)));
$ submit = new Zend_Form_Element_Submit('submit');
$ this-> addElements(array($ name,$ image,$ submit));
$ b $这是我的控制器
class Admin_RoomtypesController extends Zend_Controller_Action
{
public function addAction()
{
$ form = new Admin_Form_RoomtypeForm );
$ b $ if($ this-> getRequest() - > isPost()){
$ formData = $ this-> getRequest() - > getPost();
$ form->填充($ formData);
$ name = $ form-> getValue('name');
$ b code
$ b现在我想要将更改文件名后的文件上传为$ name值。我怎么能做到这一点?解决方案最后我做了如下。
-Form -
class Admin_Form_RoomtypeForm extends Zend_Form
{
public function init()
{
$ name = new Zend_Form_Element_Text('name');
$ name-> setLabel('Name:');
$ image = new Zend_Form_Element_File('image');
$ image-> setLabel('Image URL:');
$ image-> addValidators(array(array('IsImage',false)));
$ submit = new Zend_Form_Element_Submit('submit');
$ this-> addElements(array($ name,$ image,$ submit));
$ b $控制器 - $ /
class Admin_RoomtypesController extends Zend_Controller_Action
{
public function addAction()
{
$ form = new Admin_Form_RoomtypeForm() ;
$ b $ if($ this-> getRequest() - > isPost()){
$ formData = $ this-> getRequest() - > getPost();
$ form->填充($ formData);
$ name = $ form-> getValue('name');
$ image = $ form-> getValue('image');
$ temp = explode(。,$ image);
$ ext = $ temp [count($ temp)-1];
$ imageName = $ name。'。' $ EXT;
$ fullPath = $ imagePath。/。 $ imageName;
copy(/ tmp /。$ image,$ fullPath);
默认图片上传到/ tmp文件夹。我们可以使用 setDestination()
方法来改变它。
感谢所有人
This is my form
class Admin_Form_RoomtypeForm extends Zend_Form
{
public function init()
{
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Name :');
$imagePath = '/home/dinuka/image';
$image = new Zend_Form_Element_File('image');
$image->setLabel('Image URL :');
$image->setDestination($imagePath);
$image->addValidators(array(array('IsImage', false)));
$submit = new Zend_Form_Element_Submit('submit');
$this->addElements(array($name, $image, $submit));
}
}
This is my controller
class Admin_RoomtypesController extends Zend_Controller_Action
{
public function addAction()
{
$form = new Admin_Form_RoomtypeForm();
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
$form->populate($formData);
$name = $form->getValue('name');
}
}
}
Now i want to upload file after change file name as $name value. How can i do it?
解决方案 Finally I done it as following.
-Form -
class Admin_Form_RoomtypeForm extends Zend_Form
{
public function init()
{
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Name :');
$image = new Zend_Form_Element_File('image');
$image->setLabel('Image URL :');
$image->addValidators(array(array('IsImage', false)));
$submit = new Zend_Form_Element_Submit('submit');
$this->addElements(array($name, $image, $submit));
}
}
-Controller -
class Admin_RoomtypesController extends Zend_Controller_Action
{
public function addAction()
{
$form = new Admin_Form_RoomtypeForm();
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
$form->populate($formData);
$name = $form->getValue('name');
$image = $form->getValue('image');
$temp = explode(".", $image);
$ext = $temp[count($temp)-1];
$imageName = $name .'.'. $ext;
$fullPath = $imagePath ."/". $imageName;
copy("/tmp/". $image, $fullPath);
}
}
}
default image is upload to /tmp folder. We can change it using setDestination()
method as my example.
Thank for All
这篇关于在zend中重命名文件后上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文