0

I'm trying to upload a file through an API (Box-API) in Zend Framework 1.12. But there is a problem. I've a input type "file" in my form but i have to pass the name of the file to the box api. And the form probably doesn't register the name of the file cause when i try to get the parameter from the POST call it doesn't return anything. (I tryied to printf it to the output). The code of the form is the following:

   $form = new Zend_Form;
    $form->setAction('/imball-reagens/public/upload')
    ->setMethod('post');
    $file = new Zend_Form_Element_File('file');
    $file->setLabel('Choose a file to upload:');
    $file->addValidator('alnum');
    $file->setRequired(true);
    $form->addElement($file);
    $access_token = new Zend_Form_Element_Hidden(array('name' => 'access_token', 'value' => $result->access_token));
    $form->addElement($access_token);
    $refresh_token = new Zend_Form_Element_Hidden(array('name' => 'refresh_token', 'value' => $result->refresh_token));
    $form->addElement($refresh_token);
    $form->addElement('submit', 'upload', array('label' => 'Upload File'));
    echo $form;

The code which process the form (the code of the action called by the form) is the following:

    $access_token= $this->getRequest()->getParam('access_token');
    $client = new Zend_Http_Client('https://upload.box.com/api/2.0/files/content');
    $client->setMethod(Zend_Http_Client::POST);
    $client->setHeaders('Authorization: Bearer '.$access_token);
    $client->setParameterPost(array(
            'filename'  => '@'.$this->getRequest()->getParam('file'),
            'parent_id' => '0'
    ));

Specifically this line:

 'filename'  => '@'.$this->getRequest()->getParam('file'),

has probably to be changed cause it leaves empty the field throwing this error:

 {"type":"error","status":400,"code":"invalid_request_parameters","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Invalid input parameters in request","request_id":"116495728752d937d3c6ca5"}

I've found many ways of doing it but they were all building the form as a class, Here i'm just using Zend Form class, not building a new class for the form like in this example.

I got a little help from the zend chatroom irc. This command:

 $data = array_merge($request->getPost(), $request->getFiles())

but i don't know to which object is related the $request variable/object.

Anyone has any idea of how to solve this issue? Thanks

3
  • 1
    This link might help framework.zend.com/manual/2.1/en/modules/… Commented Jan 17, 2014 at 14:21
  • 1
    $request is the request object. Change your code to $data = array_merge($this->_request->getPost(), $this->_request->getFiles()) Commented Jan 17, 2014 at 15:03
  • @ user3165879 Call to undefined method Zend_Controller_Request_Http::getFiles() :( sorry @Patrick Q this is the manual for version 2.1 of zend framework. I'm not sure that the same class exist in Zend Framework 1.12, i'll check in my manuals during th weekend. Thank you all anyway! Commented Jan 17, 2014 at 17:05

1 Answer 1

0

Ok the right answer to this question is in this other stackoverflow question. I've worked a lot on this and found out it was a zend framework shortcut to file uploading:

 $access_token= $this->getRequest()->getParam('access_token');
 $client = new Zend_Http_Client('https://upload.box.com/api/2.0/files/content');
 $client->setMethod(Zend_Http_Client::POST);
 $client->setHeaders('Authorization: Bearer '.$access_token);
 $data = $_FILES["file"]["tmp_name"];
 $client->setParameterPost(array(
     'parent_id' => '0'
     ));
 $client->setFileUpload($data, 'filename');
 $response = $client->request()->getBody();
 $this->view->response= $response;
 $result = json_decode($response);

My 2 cents.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.