0

Hi I'm having a problem with multiple file uploading. Whenever I try to access the file properties it won't let me. I debugged my data and here's the output: Instead of returning me an array, it just returns me a string under data['files']. So I'm missing properties such as 'tmp_name', 'name', etc..

enter image description here

My code in my .ctp file :

<?= $this->Form->create('annonce') ?>
<fieldset>
    <legend><?= __('Publier une annonce') ?></legend>
    <?php
        echo $this->Form->input('title', ['label' => 'Titre annonce']);
        echo $this->Form->input('description', ['label' => 'Description annonce', 'type' => 'textarea']);
        echo $this->Form->input('price', ['label' => 'Prix ', 'type' => 'number', 'placeholder' => '0$']);
        echo $this->Form->input('type_annonce', array(
        'options' => array('Acheter', 'Vendre', 'Échanger', 'Autre'),
        'empty' => 'Choisissez une option'), ['label' => 'Je souhaite']);
        echo $this->Form->input('category', array(
        'options' => array('Cheval', 'Poney', 'Matériel', 'Pension', 'Demi-pension'),
        'empty' => 'Choisissez une option'), ['label' => 'Je souhaite']);

        echo $this->Form->input('files.', array('type' => 'file', 'multiple' => 'true', 'enctype'=>'multipart/form-data'));
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

And my controller:

    public function add()
{
    debug($this->request);
    $now = Time::now();
    $this->log('In add annonce !', 'debug');
    $annonce = $this->Annonces->newEntity();
    if ($this->request->is('post')) {
        if(!empty($this->request->data)) {
            $this->Flash->succes(__('Annonce files not empty'));
            foreach ($this->request->data['files'] as $file) {
                $filename = $file['name'];
                $file_tmp_name = $file['tmp_name'];
                $dir = WWW_ROOT.'img'.DS.'upload';
                $allowed = array('png', 'jpg', 'jpeg');

                echo "<pre>"; print_r($photo); echo "</pre>";
                if ( !in_array( substr( strrchr( $filename , '.') , 1 ) , $allowed) ) {
                    throw new InternalErrorException("Error Processing Request.", 1);       
                }elseif( is_uploaded_file( $file_tmp_name ) ){
                    move_uploaded_file($file_tmp_name, $dir.DS.Text::uuid().'-'.$filename);
                }
            }
        }
        else {
            $this->Flash->error(__('Not in images'));
        }
        $annonce = $this->Annonces->patchEntity($annonce, $this->request->data);
        $annonce->id_user = $this->request->session()->read('Auth.User.id');
        $annonce->publication_date = $now;
        if ($this->Annonces->save($annonce)) {
            $this->log('Success saving annonce !', 'debug');
            $this->Flash->success(__('The annonce has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->log($annonce->errors(), 'debug');
            $this->Flash->error(__('The annonce could not be saved. Please, try again.'));
        }
    }
    $this->set(compact('annonce'));
    $this->set('_serialize', ['annonce']);
}

2 Answers 2

3

Try this structure.

<?= $this->Form->create($annonce, ['type' => 'file']) ?>
    ...
    <?= $this->Form->input('files[]', ['type' => 'file', 'class' => 'file', 'multiple', 'label' => __('Select Images')]) ?>
    ... 
<?= $this->Form->end() ?>
Sign up to request clarification or add additional context in comments.

1 Comment

OMG ! Thank you, you saved me more hours of debugging !
2

You need to add enctype='multipart/form-data' to the form when there are files included in it. Do it like this :

<?= $this->Form->create($annonce, ['type' => 'file']) ?>

Note - you should directly use the entity as the first parameter for the form create helper function instead of a string.

2 Comments

I tried this: echo $this->Form->input('files.', array('type' => 'file', 'multiple' => 'true', 'enctype'=>'multipart/form-data')); and it still does not work :/
enctype doesn't need to be there in input. PS - just observed that other answer has worked :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.