2017-05-13 40 views
0

我使用3.4 cakephp版本,但文件上傳成功,但我希望URL在請求實體中更新。我可以更新一個實體,但不是全部: $ post-> url = WWW_ROOT.'uploads/filename';CakePHP 3.0多個文件上傳url

HTML表單:

<?= $this->Form->create(null, ['class' => 'form-horizontal', 'id' => 'postSubmit', 'type' => 'file', 'autocomplete' => 'off'])?> 
     <div class="form-group"> 
     <label for="inputEmail3" class="col-sm-4 control-label">Ad Category:</label> 
     <div class="col-sm-8"> 
      <?= $this->Form->select('category_id', $categories, ['class' => 'form-control required categorySelected', 'empty' => 'Select Category'])?> 
     </div>  
     </div> 
     <div class="form-group"> 
     <label for="inputPassword3" class="col-sm-4 control-label">Photos(5 max):</label> 
     <div class="col-sm-8"> 
      <?= $this->Form->input('images.', [ 'type' => 'file', 'id' => 'image', 'multiple' => 'multiple', 'accept' => 'image/jpg, image/jpeg', 'label' => false])?> 
      <p class="help-block">Good photos quick response</p> 
     </div> 
     </div>  

     <div class="form-group"> 
      <div class="col-sm-8 col-sm-offset-4">   
      <?= $this->Form->button('Post Now', ['class' => 'btn custom_btn btn-sm', 'type' => 'submit', 'label' => false])?> 
      </div> 
     </div> 
    </form> 

控制器動作:

$post = $this->Posts->newEntity(); 
$post = $this->Posts->patchEntity($post, $this->request->getData());   


if($this->request->getData()['images']) { 
        $dir = new Folder(WWW_ROOT.'uploads', true, 0755); 
        $files = $this->request->getData()['images']; 
        foreach ($files as $key => $file) { 
         if($file['error'] == 0) { 
          $info = pathinfo($file["name"]); 
          $newfilename = $info["filename"].'_'.time() . '.'. $info["extension"]; 
          if(move_uploaded_file($file["tmp_name"], WWW_ROOT.'uploads/' . $newfilename)) { 
           $post->url = WWW_ROOT.'uploads/' . $newfilename; 


          } 
         } 
        } 
       } 
       if ($this->Posts->save($post)) { 
        $this->Flash->success('Post has been submitted successfully. Please wait 
         for approval'); 
       } 

回答

0

我建議你實現CakePHP3-Proffer描述波紋管

Uploadi ng多個相關圖像

本示例將告訴您如何上傳與當前表類相關的許多圖像。一個示例設置可能是您有一個Users表類和一個UserImages表類。下面的例子只是baked code

的關係是建立如下。請務必將行爲附加到正在接收上傳的表類。

// src/Model/Table/UsersTable.php 
$this->hasMany('UserImages', ['foreignKey' => 'user_id']) 

// src/Model/Table/UserImagesTable.php 
$this->addBehavior('Proffer.Proffer', [ 
    'image' => [ 
     'dir' => 'image_dir', 
     'thumbnailSizes' => [ 
      'square' => ['w' => 100, 'h' => 100], 
      'large' => ['w' => 250, 'h' => 250] 
     ] 
    ] 
]); 

$this->belongsTo('Users', ['foreignKey' => 'user_id', 'joinType' => 'INNER']); 

實體

你的實體必須允許相關的領域,在它的$_accessible陣列。因此,在我們的示例中,我們需要檢查'user_images' => true是否包含在我們的用戶實體中。

控制器

需要標準控制器代碼能夠製成蛋糕將自動默認保存任何第一級相關聯的數據沒有變更。由於我們的用戶表直接與我們的UserImages表相關聯,因此我們無需更改任何內容。

如果您使用的是相關模型數據,則需要指定使用'associated'密鑰的merging the entity data時要填充的關聯。

模板

您需要使用正確的字段名稱,以便您的請求數據格式正確無誤,包括你的模板的相關領域。

// Don't forget that you need to include ['type' => 'file'] in your ->create() call 
<fieldset> 
    <legend>User images</legend> 
    <?php 
    echo $this->Form->input('user_images.0.image', ['type' => 'file']); 
    echo $this->Form->input('user_images.1.image', ['type' => 'file']); 
    echo $this->Form->input('user_images.2.image', ['type' => 'file']); 
    ?> 
</fieldset> 

你如何處理現有圖像,現有圖像的缺失的顯示,並且新上傳域的加入是你的,和這個例子的範圍之內。