/**
  * Move converted file to a new location
  * @param $destination The destination of the moved file
  */
 public function moveTo($destination)
 {
     if (isset($this->_file['in_request']) && $this->_file['in_request'] == true) {
         $data = $this->_uploadedFile->getPackage()->getPackageField(sprintf(PostFields::file, $this->_convertedFileIndex, $this->_uploadedFileIndex));
         $handle = fopen($destination, 'w');
         $result = fwrite($handle, $data);
         if ($result !== FALSE) {
             // no error
             $result = TRUE;
         }
         $result &= fclose($handle);
     } else {
         $path = $this->_file['tmp_name'];
         if (is_uploaded_file($path)) {
             $result = move_uploaded_file($path, $destination);
         } else {
             if ($this->_file['cached']) {
                 $result = UploadCache::moveFile($path, $destination);
             } else {
                 throw new Exception('File is not "is_uploaded_file" and is not cached file');
             }
         }
     }
     if (!$result) {
         throw new Exception("Unable to move file \"{$path}\" to \"{$destination}\"");
     }
 }
Example #2
0
 /**
  * Remove expired upload sessions
  */
 private function removeExpiredSessions($uploadCache)
 {
     $cacheRoot = $uploadCache->getCacheRoot();
     if (empty($cacheRoot) || !is_dir($cacheRoot)) {
         return;
     }
     $cacheRoot = rtrim($cacheRoot, '/\\');
     $lastFullScan = $uploadCache->getLastFullScanTimestamp();
     $currentTimestamp = time();
     // Do not scan all cache too often
     if ($lastFullScan + $this->_cacheAliveTimeout / 2 > $currentTimestamp) {
         return;
     }
     $dirs = scandir($cacheRoot);
     foreach ($dirs as $dir) {
         if ($dir != '.' && $dir != '..' && is_dir($cacheRoot . DIRECTORY_SEPARATOR . $dir)) {
             $uploadCache = new UploadCache($cacheRoot);
             $uploadSessionId = $dir;
             if ($uploadCache->getWriteTimestamp($uploadSessionId) + $this->_cacheAliveTimeout < $currentTimestamp) {
                 $uploadCache->cleanUploadSessionCache($uploadSessionId);
             }
         }
     }
     $uploadCache->setLastFullScanTimestamp($currentTimestamp);
 }
 private static function rmdir_recursive($dir)
 {
     if (is_dir($dir)) {
         $objects = scandir($dir);
         foreach ($objects as $object) {
             if ($object != '.' && $object != '..') {
                 if (is_dir($dir . DIRECTORY_SEPARATOR . $object)) {
                     UploadCache::rmdir_recursive($dir . DIRECTORY_SEPARATOR . $object);
                 } else {
                     unlink($dir . DIRECTORY_SEPARATOR . $object);
                 }
             }
         }
         reset($objects);
         rmdir($dir);
     }
 }