I am working in Zend Framework 1 and I have this function in a controller:
public function uploadAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$data = [];
if ($this->getRequest()->isPost()) {
$path = /cronjobs/uploads';
// Clean $path directory OOP way using SPL
$di = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($ri as $file) {
$file->isDir() ? rmdir($file) : unlink($file);
}
$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->addValidator('Extension', false, ['extension' => 'csv', 'case' => true]);
$adapter->addValidator('MimeType', false, ['extension' => 'text/plain']);
// Check if the uploaded file is valid
$errors[] = $adapter->isValid() === false ? $adapter->getMessages() : '';
$file = (array) $adapter->getFileInfo()['file'];
$ext = end(explode('.', $file['name']));
$new_path = $file['tmp_name'];
// Check file size
$checkFileSize = Attachment::checkMaxfileSize($file['size']);
if (!$checkFileSize['accept']) {
echo json_encode($checkFileSize['message']);
return true;
}
$data['file'] = array(
'name' => $file['name'],
'size' => $adapter->getFileSize(),
'file_path' => $new_path,
'file_ext' => $ext
);
$data['var'] = '';
} else {
$data['error'] = 'Invalid request.';
}
return $this->_helper->json($data);
}
This method is called trough AJAX as follow:
$('#fileupload').show().fileupload({
url: url,
type: "POST",
cache: false,
dataType: 'json',
done: function (e, data) {
console.log(data.result);
},
error: function (xhr, textStatus, errorThrown) {
console.log(xhr + " " + textStatus + " " + errorThrown);
}
})
For some reason as soon as I call $adapter->isValid()
in the controller the AJAX response gets break. I can say the problem is there because if I comment that piece of code everything works fine.
This is the message I am getting currently:
POST http://localhost/admin/upload net::ERR_EMPTY_RESPONSE
massive_charge_types_file_upload.js:147 [object Object] error
After read all of the following topics:
- net::ERR_EMPTY_RESPONSE when post with ajax
- Getting ERR_EMPTY_RESPONSE on $.POST
- PHP Jquery Ajax call throws net::ERR_EMPTY_RESPONSE
- and many more in Google
I am out of ideas and I am stuck since can't find what's causing the behavior.
UPDATE:
I believe the problem is on the isValid()
method which return a boolean but for some reason this is breaking my response. Any ideas?
Can any help me with this?
It seems the syntax of your MimeType validator is wrong:
Should probably be:
As described:
https://framework.zend.com/manual/1.12/en/zend.file.transfer.validators.html
Since your file won't pass the (impossible) validation test - I am guessing this is what then leads to no results?