1

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
  • 1
    looks like you explicitly need at $model there. try associating it with a model. Commented Aug 11, 2009 at 2:48

3 Answers 3

2

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' )); 
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
0

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.