Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP: posted file data not included in request->data

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?

like image 351
emersonthis Avatar asked Jul 12 '13 19:07

emersonthis


1 Answers

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']);
like image 108
ndm Avatar answered Nov 01 '22 21:11

ndm