0
votes

I am using 3.4 cakephp version but file uploaded successfully but i want the url to be updated in request entity. I can have one entity updated but not all like: $post->url = WWW_ROOT.'uploads/filename';

Html form :

 <?= $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>

Controller Action :

 $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');
                }
1

1 Answers

0
votes

I recommend you to implement the CakePHP3-Proffer described bellow:

Uploading multiple related images

This example will show you how to upload many images which are related to your current table class. An example setup might be that you have a Users table class and a UserImages table class. The example below is just baked code.

Tables

The relationships are setup as follows. Be sure to attach the behavior to the table class which is receiving the uploads.

// 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']);

Entities

Your entity must allow the associated field in it's $_accessible array. So in our example we need to check that the 'user_images' => true is included in our User entity.

Controller

No changes need to be made to standard controller code as Cake will automatically save any first level associated data by default. As our Users table is directly associated with our UserImages table, we don't need to change anything.

If you were working with a related models data, you would need to specify the associations to populate when merging the entity data using the 'associated' key.

Templates

You will need to include the related fields in your templates using the correct field names, so that your request data is formatted correctly.

// 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>

How you deal with the display of existing images, deletion of existing images, and adding of new upload fields is up to you, and outside the scope of this example.