1

新しい 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 にアクセスしようとしているというエラーが発生しました....

4

0 に答える 0