我的控制器代码如下:
$form_company=new Project_Form_AddCompany();
$this->view->form=$form_company;
$form_company->setAction('add-Company');
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form_company->isValid($formData)) {
$company_name = $this->_getParam('company', 0);
echo $company_name;
// success - do something with the uploaded file
$uploadedData = $form_company->getValues();
$form_company->uploaded_file->addFilter(new Zend_Filter_File_Rename(array('target' => $company_name.'.jpg')));
Zend_Debug::dump($uploadedData, '$uploadedData');
//echo 'End: ';
}
// action body
}
在表格中,我有以下代码:
<?php
class Project_Form_AddCompany extends Zend_Form
{
public function init()
{
$company=new Zend_Form_Element_Text('company');
$company->setLabel('CompanyName')
->setRequired(true);
$com_des=new Zend_Form_Element_Textarea('com_des');
$com_des->setLabel('CompanyDescription')
->setAttrib('cols', '40')
->setAttrib('rows', '4')
->setAttrib('style', 'resize: none;')
->setRequired(true)
->addValidator('NotEmpty');
$com_addr=new Zend_Form_Element_Textarea('com_addr');
$com_addr->setLabel('CompanyAddress')
->setAttrib('cols', '40')
->setAttrib('rows', '4')
->setAttrib('style', 'resize: none;')
->setRequired(true)
->addValidator('NotEmpty');
$file = new Zend_Form_Element_File('uploaded_file');
$file->setDestination(PUBLIC_PATH . '/images/')
->setLabel('File')
->setRequired(true)
->addValidator('NotEmpty');
$submit = new Zend_Form_Element_Submit('submit');
$this->addElements(array($company,$com_des,$com_addr,$file,$submit));
}
}
因为我必须将上传的图像重命名为$ company_name.png,这就是为什么我在控制器中使用addFilter方法,其中$ company_name是用户输入的textfield company(在表单中)的值。但是名称尚未更改。重命名应该做什么??
答案 0 :(得分:0)
您在检索表单值后添加过滤器,这就是它未被应用的原因。
在检索上传的文件后调用move_uploaded_file() PHP函数会不会更简单?
如下所示:
if (!$form_company->uploaded_file->receive()) {
// Error!
}
$location = $form_company->uploaded_file->getFileName();
move_uploaded_file($location, $yourLocation . $company_name.'.jpg';
如果没有,则始终可以使用rename()
将上传的文件重命名为您需要的任何名称。
希望有所帮助,
答案 1 :(得分:0)