Reputation: 10712
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?
Upvotes: 1
Views: 1642
Reputation: 917
/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 :-)
Upvotes: 0
Reputation: 1773
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
Upvotes: 0
Reputation: 521995
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'
));
Upvotes: 2