Exemplo n.º 1
0
 /**
  * Upload file.
  *
  * @param array $params
  * @return array
  */
 public function actionUpload($params)
 {
     if (!isset($_FILES['file'])) {
         throw new Exception('No file to upload.');
     }
     $result = array('status' => 1, 'overwrite' => array(), 'uploaded_virtual' => array(), 'uploaded_public' => array());
     $virtualPath = $params['path'];
     if ($params['public'] == 'true') {
         $virtualPath = self::publicToVirtual($virtualPath);
     }
     $targetPath = self::virtualToPhysical($virtualPath);
     if (!self::isPhysicalWritable($targetPath)) {
         throw new Exception('Access denied');
     }
     if (is_file($targetPath)) {
         $virtualPath = dirname($virtualPath);
         $targetPath = dirname($targetPath);
     }
     $overwrite = array();
     foreach ((array) $_FILES['file']['error'] as $key => $error) {
         if ($error) {
             throw new Exception('Upload error: ' . Helper::uploadCodeToMessage($error));
         }
         $name = self::filterFilename($_FILES['file']['name'][$key]);
         $source = $_FILES['file']['tmp_name'][$key];
         $target = $targetPath . '/' . $name;
         if (file_exists($target)) {
             $targetHash = sha1_file($target);
             $sourceHash = sha1_file($source);
             if ($targetHash !== $sourceHash) {
                 $result['overwrite'][] = $name;
                 $result['status'] = 0;
                 $overwrite[$name] = array('target' => $target, 'temp' => $sourceHash);
                 $target = $this->app['tempPath'] . DIRECTORY_SEPARATOR . $sourceHash;
                 move_uploaded_file($source, $target);
                 continue;
             }
         } else {
             move_uploaded_file($source, $target);
         }
         $result['uploaded_virtual'][] = $virtualPath . '/' . $name;
         $result['uploaded_public'][] = self::physicalToPublic($target);
     }
     $ses = new \Zend\Session\Container(__CLASS__);
     $ses->uploadOverwrite = $overwrite;
     return $result;
 }