Php symfony 1.4前端图像上传
我开始了新的symfony1.4项目,一切都很好,直到我决定在前端上传一个图像。似乎不可能。谷歌表示,经过一些搜索后,图像上传在嵌入式表单或类似的东西中无法工作 让我描述一下问题: 在lib/form/doctor/base中,我有我的基类:Php symfony 1.4前端图像上传,php,image,doctrine,symfony-1.4,Php,Image,Doctrine,Symfony 1.4,我开始了新的symfony1.4项目,一切都很好,直到我决定在前端上传一个图像。似乎不可能。谷歌表示,经过一些搜索后,图像上传在嵌入式表单或类似的东西中无法工作 让我描述一下问题: 在lib/form/doctor/base中,我有我的基类: abstract class BaseAlbumimagesForm extends BaseFormDoctrine { public function setup() { $this->setWidgets(array(
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';
}
}
以及扩展此基类的类AlbumImage。。。symfony做的一切都是泛型的,我所做的就是编辑输入文件和它的验证器,这在后端工作得非常完美
在前端,我想要相同的表单,所以在我的模块/操作中,我有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();
}}}
这将保存除数据库中的路径字段和上载目录中的实际图像之外的所有内容
谷歌找到了这个代码
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
,因为表单值路径没有发送该文件….您的前端表单多部分/表单数据吗?这样想……这就是为什么像后端一样的图像上载无法正常工作的原因?