2017-05-13 43 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> 

你如何处理现有图像,现有图像的缺失的显示,并且新上传域的加入是你的,和这个例子的范围之内。