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?
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']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With