سؤال

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

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top