Example #1
0
 public function indexAction()
 {
     $request = $this->getRequest();
     if (!$request->isPost()) {
         $this->getHelper('Redirector')->goto('index', 'index');
         // action, controller
     }
     $c = Zend_Registry::get('config');
     $apiKey = Zend_Filter::get($request->getPost('apikey'), 'Alnum');
     try {
         $user = User::findByApiKey($apiKey);
         if ($user === null) {
             $this->view->assign('response', 'Invalid API key.');
         } else {
             $user->setIp($request->getServer('REMOTE_ADDR'));
         }
     } catch (Zend_Db_Adapter_Exception $e) {
         $this->view->assign('response', $e->getMessage());
     }
     if (isset($user)) {
         if ($_FILES['file']['error'] === 0) {
             $file = new File();
             $file->setFileName($_FILES['file']['name']);
             $file->setFileSize($_FILES['file']['size']);
             $file->setTmpName($_FILES['file']['tmp_name']);
             $file->setUploadedBy($user);
             try {
                 $url = $file->save();
                 $this->view->assign('response', $url . "\n");
             } catch (Exception $e) {
                 $this->view->assign('response', $e->getMessage());
             }
         } else {
             switch ($_FILES['file']['error']) {
                 case UPLOAD_ERR_OK:
                     break;
                 case UPLOAD_ERR_INI_SIZE:
                     throw new Exception('The uploaded file exceeds the upload_max_filesize directive (' . ini_get('upload_max_filesize') . ') in php.ini.');
                     break;
                 case UPLOAD_ERR_FORM_SIZE:
                     throw new Exception('The uploaded file exceeds the MAX_FILE_SIZE directive' . 'that was specified in the HTML form.');
                     break;
                 case UPLOAD_ERR_PARTIAL:
                     throw new Exception('The uploaded file was only partially uploaded.');
                     break;
                 case UPLOAD_ERR_NO_FILE:
                     throw new Exception('No file was uploaded.');
                     break;
                 case UPLOAD_ERR_NO_TMP_DIR:
                     throw new Exception('Missing a temporary folder.');
                     break;
                 case UPLOAD_ERR_CANT_WRITE:
                     throw new Exception('Failed to write file to disk.');
                     break;
                 default:
                     throw new Exception('Unknown File Error.');
             }
         }
     }
 }
Example #2
0
 /**
  * Create a file instance from a file info array ($_FILES)
  * 
  * @param array $fileInfo
  *
  * @return File The created file
  */
 public static function createFromArray(array $fileInfo)
 {
     $file = new File();
     $file->setName(isset($fileInfo['name']) ? $fileInfo['name'] : null);
     $file->setSize(isset($fileInfo['size']) ? $fileInfo['size'] : 0);
     $file->setType(isset($fileInfo['type']) ? $fileInfo['type'] : null);
     $file->setTmpName(isset($fileInfo['tmp_name']) ? $fileInfo['tmp_name'] : null);
     $file->setError(isset($fileInfo['error']) ? $fileInfo['error'] : 0);
     return $file;
 }