2016-06-23 10 views
0

こんにちは私は複数のファイルのアップロードに問題があります。私がファイルのプロパティにアクセスしようとするたびに、それは私に許されません。私は自分のデータをデバッグし、出力は次のようになります: 配列を返すのではなく、データ['files']の下に文字列を返します。だから、私は.ctpファイル内の私のコードはCakePHP 3:配列の代わりに複数のファイルのアップロードが返される

enter image description here

...たとえば 'tmp_name'、 '名前'、などのような特性を欠けている:

<?= $this->Form->create('annonce') ?> 
<fieldset> 
    <legend><?= __('Publier une annonce') ?></legend> 
    <?php 
     echo $this->Form->input('title', ['label' => 'Titre annonce']); 
     echo $this->Form->input('description', ['label' => 'Description annonce', 'type' => 'textarea']); 
     echo $this->Form->input('price', ['label' => 'Prix ', 'type' => 'number', 'placeholder' => '0$']); 
     echo $this->Form->input('type_annonce', array(
     'options' => array('Acheter', 'Vendre', 'Échanger', 'Autre'), 
     'empty' => 'Choisissez une option'), ['label' => 'Je souhaite']); 
     echo $this->Form->input('category', array(
     'options' => array('Cheval', 'Poney', 'Matériel', 'Pension', 'Demi-pension'), 
     'empty' => 'Choisissez une option'), ['label' => 'Je souhaite']); 

     echo $this->Form->input('files.', array('type' => 'file', 'multiple' => 'true', 'enctype'=>'multipart/form-data')); 
    ?> 
</fieldset> 
<?= $this->Form->button(__('Submit')) ?> 
<?= $this->Form->end() ?> 

そして、私のコントローラ:

public function add() 
{ 
    debug($this->request); 
    $now = Time::now(); 
    $this->log('In add annonce !', 'debug'); 
    $annonce = $this->Annonces->newEntity(); 
    if ($this->request->is('post')) { 
     if(!empty($this->request->data)) { 
      $this->Flash->succes(__('Annonce files not empty')); 
      foreach ($this->request->data['files'] as $file) { 
       $filename = $file['name']; 
       $file_tmp_name = $file['tmp_name']; 
       $dir = WWW_ROOT.'img'.DS.'upload'; 
       $allowed = array('png', 'jpg', 'jpeg'); 

       echo "<pre>"; print_r($photo); echo "</pre>"; 
       if (!in_array(substr(strrchr($filename , '.') , 1) , $allowed)) { 
        throw new InternalErrorException("Error Processing Request.", 1);  
       }elseif(is_uploaded_file($file_tmp_name)){ 
        move_uploaded_file($file_tmp_name, $dir.DS.Text::uuid().'-'.$filename); 
       } 
      } 
     } 
     else { 
      $this->Flash->error(__('Not in images')); 
     } 
     $annonce = $this->Annonces->patchEntity($annonce, $this->request->data); 
     $annonce->id_user = $this->request->session()->read('Auth.User.id'); 
     $annonce->publication_date = $now; 
     if ($this->Annonces->save($annonce)) { 
      $this->log('Success saving annonce !', 'debug'); 
      $this->Flash->success(__('The annonce has been saved.')); 
      return $this->redirect(['action' => 'index']); 
     } else { 
      $this->log($annonce->errors(), 'debug'); 
      $this->Flash->error(__('The annonce could not be saved. Please, try again.')); 
     } 
    } 
    $this->set(compact('annonce')); 
    $this->set('_serialize', ['annonce']); 
} 

答えて

2

この構造を試してください。

<?= $this -> Form -> create($annonce, ['type' => 'file']) ?> 
    ... 
    <?= $this -> Form -> input('files[]', ['type' => 'file', 'class' => 'file', 'multiple', 'label' => __('Select Images')]) ?> 
    ... 
<?= $this -> Form -> end() ?> 
+0

OMG!ありがとう、あなたは私にデバッグの時間を節約しました! – Melissa

2

ファイルが含まれている場合はenctype = 'multipart/form-data'をフォームに追加する必要があります。 thisようにそれを実行します。

<?= $this->Form->create($annonce, ['type' => 'file']) ?> 

注 - あなたは直接フォームはヘルパー関数の代わりの文字列を作成するための最初のパラメータとしての実体を使用する必要があります。

+0

私はこの試みた:= '複数の' => '本当'、 'のenctype'、 'エコーます$ this->フォーム - >入力( 'ファイル'、配列( 'タイプ' => 'ファイルを' > 'multipart/form-data')); 'それでも動作しません:/ – Melissa

+0

enctypeは入力の必要はありません。 PS - ちょうど他の答えが働いていることがわかりました:) – Gaurav

関連する問題