2016-12-03 12 views
0

cakephp 3.3を使用しています。私は単一の画像をアップロードすることができますが、ウェブルート/アップロードの下で保存されていません。私は複数の画像をアップロードして保存したい。どうすれば達成できますか?いくつかの入力を提供してください。私は非常にPHPプログラミングとこのフレームワークに新しいです。ありがとう! add.ctpで複数の画像をアップロードしてcakephpを使用してサーバに保存する方法3.3

 `Images\add.ctp 
     <?= $this->Form->create($image,['type' => 'file']) ?> 
     <fieldset> 
     <legend><?= __('Add Image') ?></legend> 
     <?php 

     echo $this->Form->input('path',['type' => 'file']); 
     ?> 
     </fieldset>` 

     ImagesController\add 

     public function add() 
{ 
    $image = $this->Images->newEntity(); 
    if ($this->request->is('post')) { 
     $image = $this->Images->patchEntity($image, $this->request->data); 
     if ($this->Images->save($image)) { 
      $this->Flash->success(__('The image has been saved.')); 

      return $this->redirect(['action' => 'index']); 
     } else { 
      $this->Flash->error(__('The image could not be saved. Please, try again.')); 
     } 
    } 
    $properties = $this->Images->Properties->find('list', ['limit' => 200]); 
    $this->set(compact('image', 'properties')); 
    $this->set('_serialize', ['image']); 
} 

      Table\ImageTable.php 

     $validator 
     ->requirePresence('path', 'create') 
     ->notEmpty('path') 
    ->add('processImageUpload', 'custom', [ 
     'rule' => 'processImageUpload' 
    ]); 

public function processImageUpload($check = array()) { 
if(!is_uploaded_file($check['path']['tmp_name'])){ 
    return FALSE; 
} 
if (!move_uploaded_file($check['path']['tmp_name'], WWW_ROOT . 'img' . DS . 'images' . DS . $check['path']['name'])){ 
    return FALSE; 
} 
$this->data[$this->alias]['path'] = 'images' . DS . $check['path']['name']; 
return TRUE; 

}

+0

そんなに何をしようとしませんでした遠い?進捗状況も投稿してください。 –

+0

add.ctpに入力タイプファイルを作成してから、move_uploded_file()メソッドを使用してアップロードを処理しました。私は上記のコードを追加しました。ありがとう! – Mythri

答えて

0

などの入力フィールドに何か作る:

echo $this->Form->input('path[]',['type' => 'file','multiple'=>'multiple']); 

とコントローラ内のメソッド保存します。このような何か:

// $image = $this->Images->newEntity(); 
$images= $this->Articles->newEntities($this->request->data); 
if ($this->request->is('post')) { 
    // $image = $this->Images->patchEntity($image, $this->request->data); 
    if ($this->Images->saveMany($images)) { 
     $this->Flash->success(__('The image has been saved.')); 

     return $this->redirect(['action' => 'index']); 
    } else { 
     $this->Flash->error(__('The image could not be saved. Please, try again.')); 
    } 
} 
+0

ありがとう!私は、各画像の詳細を取得し、データベースに格納するために '$画像'をループする必要があります。 Webroot \ uploadsの下にあるImageを保存します。 $ images = $ this-> Images-> newEntities($ this-> request-> data);私にイメージの配列を与えます。しかし、ファイル名を取得しようとするとインデックスが見つかりません。 – Mythri

+0

@ Manohar Khadka:私はこの方法を試しました!うまく行かなかった。それはエラーとしてテーブルクラスのエントリを見つけることができません。 – Mythri

関連する問題