Question

i am using the jQuery-File-Upload jquery plugin to upload some files i am uploading the pictures from one domain (client side) to another one (server side) through an api.

the image seems to upload fine int he tmp directory on the server side but the $_FILE var doesn't contain the image type or size

array (size=1)
  'files' => 
    array (size=5)
      'name' => 
        array (size=1)
          0 => string '3.jpg' (length=5)
      'type' => 
        array (size=1)
          0 => null
      'tmp_name' => 
        array (size=1)
          0 => string '/tmp/phpXyHG5T' (length=14)
      'error' => 
        array (size=1)
          0 => int 0
      'size' => 
        array (size=1)
          0 => null

the js is simple

$('#fileupload').fileupload({
        dataType: 'json',
        url : 'http://server_side.com/requests/index/image-upload/',
        add: function (e, data) {
            data.context = $('#upload_button')
                .click(function () {
                    data.context = $('#upload_button').text('Uploading...');
                    data.submit();
                });
        },
        done: function (e, data) {
            console.log(data);
        }
    }).on('fileuploaddone', function (e, data) {
        console.log(data);
    }).on('fileuploadsubmit', function (e, data) {
        console.log(data);
    });

the php side

$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(TRUE);

// initialize the upload
$uploadHandler = new Api_Model_UploadHandler();
$uploadHandler->initialize();

any ideas?

Was it helpful?

Solution

i figure it out. I'm using Zend Framework.

the issue is that the $_FILE looses the size and type if you ask for it after the Api_Model_UploadHandler initialization because the tmp image gets removed.

so by asking for the image before seems to work ok.

no need to check if the file exists

public function imageUploadAction()
{
    // in case some other crap goes wrong on the website, i want a clean json response
    error_reporting(0);

    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender(TRUE);

    // get the request
    $request   = $this->getRequest();
    // get other params if you sent any

    $fileName = !empty($_FILES) ? $_FILES["files"]["name"][0] : '_';

    if (file_exists('tmp/' . $fileName)) {

        $upload = new Zend_File_Transfer_Adapter_Http();

        if (is_null($files)) {
            $files = $upload->getFileInfo();
        }

                // ... do a regular zend image upload
    }

    // initialize the upload
    $uploadHandler = new Api_Model_UploadHandler();
    $uploadHandler->initialize();

    return json_encode(array());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top