我开始了新的symfony 1.4项目,一切顺利,直到我决定在前端上传图片。似乎不可能。一些搜索图像上传后无法使用嵌入式表格或类似Google的说法。
让我来描述一下这个问题:
在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);`
但是我错误地试图在非对象$file
上访问getOriginalName,因为表单值路径没有发送文件....