我开始了新的 symfony 1.4 项目,一切都很好,直到我决定在前端上传一张图片。似乎不可能。经过一些搜索后,图像上传在嵌入式表单或类似谷歌的东西中不起作用。
让我描述一下问题:
在 lib/form/doctrine/base 我有我的基类:
abstract class BaseAlbumimagesForm extends BaseFormDoctrine {
public function setup() {
$this->setWidgets(array(
'id' => new sfWidgetFormInputHidden(),
'title' => new sfWidgetFormInputText(),
'path' => new sfWidgetFormInputFile(),
'description' => new sfWidgetFormInputText(),
'alt' => new sfWidgetFormInputText(),
'album_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Albums'), 'add_empty' => false)),
'ordering' => new sfWidgetFormInputText(),
'rating' => new sfWidgetFormInputText(),
'cdate' => new sfWidgetFormDate(),
'slmjeseca' => new sfWidgetFormInputCheckbox(),
'published' => new sfWidgetFormInputCheckbox(),
));
$this->setValidators(array(
'id' => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
'title' => new sfValidatorString(array('max_length' => 255)),
'path' => new sfValidatorFile(array(
'required' => false,
'path' => sfConfig::get('sf_upload_dir') . '/profiles',
'mime_types' => 'web_images',)),
'description' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
'alt' => new sfValidatorString(array('max_length' => 255, 'required' => false)),
'album_id' => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Albums'))),
'ordering' => new sfValidatorInteger(),
'rating' => new sfValidatorString(array('max_length' => 255)),
'cdate' => new sfValidatorDate(),
'slmjeseca' => new sfValidatorBoolean(),
'published' => new sfValidatorBoolean(),
));
$this->widgetSchema->setNameFormat('albumimages[%s]');
$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
$this->setupInheritance();
parent::setup();
}
public function getModelName() {
return 'Albumimages';
}
}
和类 Albumimages 扩展了这个基类......所有这些都被 symfony 通用化了,我所做的只是编辑 InputFile 和它的验证器,这在后端工作得很好。
在前端我想要相同的表单,所以在我的模块/操作中我有 action.class.php 代码
public function executeAddimage(sfWebRequest $request) {
$user_id = $this->getContext()->getUser()->getAttribute('user_id', '', 'sfGuardSecurityUser');
$this->form1 = new AlbumimagesForm();
if ($request->isMethod('post')) {
$this->form1->bind($request->getParameter($this->form1->getName()), $request->getFiles($this->form1->getName()));
if ($this->form1->isValid()) {
$this->form1->save();
}}}
这将保存数据库中的 PATH 字段和上传目录中的实际 IMAGE 以外的所有内容
谷歌搜索找到了这段代码
foreach ($request->getFiles() as $fileName) {
$theFileName = $fileName['name'];
$uploadDir = sfConfig::get("sf_upload_dir");
$Contacts_uploads = $uploadDir.'/profile';
if(!is_dir($Contacts_uploads))
mkdir($Contacts_uploads, 0777);
move_uploaded_file($fileName['tmp_name'],
"$Contacts_uploads/$theFileName");`
对我不起作用或者我没有很好地实现它
甚至尝试手动完成
$file = $form1->getValue('path');
$filename = 'path_'.sha1($file->getOriginalName());
$extension = $file->getExtension($file->getOriginalExtension());
$file->save(sfConfig::get('sf_upload_dir').'/'.$filename.$extension);`
但是我试图在非对象上访问 getOriginalName 时出现错误,$file
因为表单值路径没有发送文件....