2012-01-28 8 views
0

Iは、コントローラは、基本的にサーバ上に画像をアップロードするアクション追加ビューからコントローラに変数を渡すための適切なMVCモデルは何ですか?

機能追加($ NUMBER_OF_IMAGES = 1)を有する有し{

$this->set('number_of_images', $number_of_images); 
    if (!empty($this->data)) { 
     $count = 1; 
     foreach($this->data['Images'] as $entry){ 
      $file_name = "file" . $count;  
      if ($data_s = $this->Uploader->upload($file_name)) { 

       $this->Image->saveAll($data_s); 


      } 
      $count++;  
     } 

     $this->Session->setFlash("Your image(s) has been saved"); 
     $this->redirect(array('action'=>'index')); 



    } 
} 

マイ追加ビューは、基本的にユーザが選択できます1ファイルをアップロードしてアップロードする

<?php 

echo $this->Form->create('Images', array('type' => 'file', 'url' => array('controller' => 'images', 'action' => 'add'))); 

$count = 1; 
while($count <= $number_of_images){ 
    // file name is file + image number 
    $file_name = 'file' . $count; 
    echo $this->Form->input($file_name, array('type' => 'file')); 
    // echo $this->Form->input("File " . $count, array('type' => 'file')); 
    $count++; 
} 

echo $this->Form->end('Upload'); 

echo $this->Html->link('Go Back To Main Page', '/images') 

?> 

1つ以上のファイルを選択できるようにすることができます。 たとえば、cakephp/images/add/3に行くと、3つのアップロード選択フォームが作成されます。ここでadd.ctp

答えて

0

に、このオプションを実装するための最良の方法だろう何

は、あなたがこれを行うことができる方法を示す簡単な例です。このフォームにはモデルデータは含まれていませんが、変更するのは簡単です。 $this->data['fileinfo']は、n要素の配列になります。

コントローラー:

function add() {  
    if(!empty($this->data)) { 
     foreach($this->data['fileinfo'] as $f) { 
      switch($f['error']) { 
       case 4: 
        // nothing to do (i.e. user did not select a file) 
        break; 
       case 0: 
        // handle uploaded file 
        break; 
       default: 
        // report error message 
      } 
     } 
    } 

    $this->set('n', 5); // number of files that can be uploaded  
} 

ビュー:

<? 
echo $this->Form->create(false, array('enctype' => 'multipart/form-data')); 
for($i = 0; $i < $n; $i++) { 
    echo $this->Form->file("fileinfo.$i"); 
} 
echo $this->Form->end('Upload'); 
?> 
関連する問題