开发者

Using CakePHP to create file upload, all $file array entries return single first character of filename

开发者 https://www.devze.com 2023-02-04 16:27 出处:网络
Using the following code in my view file (add.ctp): <div class=\"resources form\"> <?php echo $this->Form->create(\'Resource\');?>

Using the following code in my view file (add.ctp):

<div class="resources form">
<?php echo $this->Form->create('Resource');?>
    <fieldset>
        <legend><?php __('Add Resource'); ?></legend>
    <?php
        echo $this->Form->input('title');
        echo $this->Form->input('file', array('type' => 'file'));
        ...
    ?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
...

...and the following code in the controller...

function add() {
    if (!empty($this->data)) {
        $this->Resource->create();
        if ($this->uploadFile() && $this->Resource->save($this->data)) {
            $this->Session->setFlash(__('The resource has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The resource could not be saved. Please, try again.', true));
        }
    }
    ....
}

...with the following function to test the process:

function uploadFile() {
        $file=$this->data['Resource']['file'];
        $this->data['Resource']['filename'] = 'failing with name:'.$file['name'].'/ file_mime: '.$file['filemime'];
    return true;
}

The database is always populated with the first character of the files original filename, regardless of what file I try to upload or what elements of the $file array I try to access. For example with test.gif I get:

"failing with name:t/ file_mime: t"

Worthy of note, this code is the result of me reducing the tasks the function was to perform down to the barest minimum to locate the problem. Hence the weirdness of the result. The database column for the filename is established like this:

filename/varchar(200)/utf8_general_ci/NOT NULL

I'm new to CakePHP and am at a total loss of what to look for to make this work, tearing my hair out. Any help o开发者_如何学编程r pointing in the right direction to look would be gratefully received.


I managed to get hold of one of the contributors to CakePHP who very, very kindly offered his help and solved the issue for me, I am very grateful but will keep him anonymous!

At a glance, I can see that your add.ctp needs an array in the form creation:

echo $this->Form->create('Resource', array('type' => 'file'));

This tells the CakePHP form helper to create a multi-part form type, ready for sending files.

0

精彩评论

暂无评论...
验证码 换一张
取 消