6

I'm trying to upload a file to 3rd party endpoint, but I can't post the file directly from my form because the API requires an api_key which I can't expose to the end user. Therefore, my plan was to point the form to a controller/action and post the data from there. However, when I debug($this->request->data) from inside the controller, the file data is missing.

The form on the view:

echo $this->Form->create('Media', array('type'=>"file", 'url'=>array('controller'=>'media', 'action'=>'upload') ) );
echo $this->Form->input('name', array("name"=>"name") );
echo $this->Form->input('file', array('type'=>'file', "name"=>"file") );
echo $this->Form->input('project_id', array('type'=>'hidden', "name"=>"project_id", "value"=>$project["Project"]['hashed_id']) );
//THIS CANNOT BE HERE: echo $this->Form->input('api_password', array('type'=>'hidden', "name"=>"api_password", "value"=>'xxxxxxx') );
echo $this->Form->end("Submit");

Here's what what I see when I debug() the request data from the controller:

array(
    'name' => 'Some Name',
    'project_id' => 'dylh360omu',
)

What's going on here?

emersonthis
  • 32,822
  • 59
  • 210
  • 375
  • have you tried the exact same, but not changing the input names? – Nunser Jul 12 '13 at 19:46
  • 2
    Have you tried looking at what is in $this->request? `debug($this->request)` I thing file data is managed outside of the request data. – usumoio Jul 12 '13 at 19:48
  • how large is the file and is it allowed to be of that size by the upload_max_filesize and the max_post_size directives? – Alex Stallen Jul 12 '13 at 19:52
  • @I am John Galt: file data is handled in $this->request->data – Alex Stallen Jul 12 '13 at 19:58
  • @Nunser: haven't tried that but I will. Maybe a bug? Is your understanding that it should normally be present in the data array? – emersonthis Jul 12 '13 at 20:02
  • Yes, I've used the file-form the same way you describe (with the exception of the naming part, that's why I asked if you've tried removing that) and the file should be in `$this->request->data['Media']['file'] without the need of changing the form names by hand... Have you tried also changing `file` for `doc` or other? maybe `file` is a reserved word? (try not forcing the names first). – Nunser Jul 12 '13 at 20:06
  • @Nunser: the file field isn't reserved – kicaj Jul 12 '13 at 21:12
  • @Emerson Can You write whick kind of Cake You're using and show Your controller action 'Upload'. And for what You're using name param in input at secend param? Remeber, the first param of input is name of field – kicaj Jul 12 '13 at 21:14

2 Answers2

8

File upload data can only be found in CakeRequest::$data in case the input element name is passed in an array named data (which is the default when not defining a specific name manually), ie:

<input type='file' name='data[file]'>

In your case however, the element will look like this:

<input type='file' name='file'>

which will cause the files data to be put in CakeRequest::$params[form].

https://github.com/cakephp/cakephp/blob/2.4.0/lib/Cake/Network/CakeRequest.php#L346

So either change the name in the form accordingly:

$this->Form->input('file', array('type' => 'file', 'name' => 'data[file]'));

Or access the file data via CakeRequest::$params[form]:

debug($this->request->params['form']);
ndm
  • 59,784
  • 9
  • 71
  • 110
0

Nunser was right (as always)! The problem resulted from customizing the name of the input. When I remove the 'name'=>'...' from the options array, the file shows up as expected. This seems like a bug, but if someone has a better explanation I'd love to hear it.

emersonthis
  • 32,822
  • 59
  • 210
  • 375
  • Hi. I'm glad is working now. ndm's answer seems to be the reason for the problem, though. It's not a bug as he explains, just "something that happens". – Nunser Jul 25 '13 at 17:36