views:

635

answers:

3

Hi all! I'm trying to upload a file with cakePHP. This is the view:

echo $form->create(null, array('action' => 'upload', 'type' => 'file'));
echo $form->file('img');
echo $form->submit('Enviar Imagem');
echo $form->end();

And this is the error I'm getting:

Warning (2): Invalid argument supplied for foreach() [CORE/cake/dispatcher.php, line 314]

Edit: cakePHP's debug tells me that these are the lines of code where the problem happens:

foreach ($_FILES['data'] as $key => $data) {
    foreach ($data as $model => $fields) {
        foreach ($fields as $field => $value) {

And this is the call stack:

Dispatcher::parseParams() - CORE/cake/dispatcher.php, line 314
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 118
[main] - APP/webroot/index.php, line 88

My upload function is currently empty. What is happening?

+1  A: 

It seems the Dispatcher expects model names to be present when checking uploaded data. I don't know if there's a good reason for that or if it's just an oversight. Anyway, just use a made up model name, it doesn't matter. It will satisfy the Dispatcher by creating a data structure it expects.

echo $form->create('File', array(
    'url'  => array('controller' => 'myController', 'action' => 'upload'),
    'type' => 'file'
));
deceze
Tried to add the controller explicitly, since this form has no associated model. But the problem remains the very same. I changed the debug level to 3 and it seems that the aforementioned foreach is dealing with data from file fields. I will update the question with more information.
Rafael Almeida
It seems that you can create forms without models in general, but the dispatcher happens to expect a model name for when checking file fields. Seems like an internal inconsistency. It doesn't matter, just add any made up model name.
deceze
A: 

When uploading files cake nukes $_POST and puts everything into

$this->params['form']

array. do a var_dump on $this->params in your upload view/controller and it should be there

Plugawy
A: 

/app/views/images/upload.ctp

echo $form->create('Image', array('action' => 'upload', 'type' => 'file'));
echo $form->file('img');
echo $form->submit('Enviar Imagem');
echo $form->end();

/app/controllers/images_controller.php

function upload(){
  if(!empty($this->data)){
    pr($this->data);
  }
}

You'll find a lot of interesting :-)

Aziz

related questions