$maxBytes) { fclose($srcHandle); fclose($dstHandle); throw new SimpleException( 'File is too big (maximum size: %s)', TextHelper::useBytesUnits($maxBytes)); } } } finally { fclose($srcHandle); fclose($dstHandle); chmod($dstPath, 0644); } } public static function mockForDownload($url, $sourceFile) { self::$mocks[$url] = $sourceFile; } public static function moveUpload($srcPath, $dstPath) { if ($srcPath == $dstPath) throw new SimpleException('Trying to move file to the same location'); if (is_uploaded_file($srcPath)) { move_uploaded_file($srcPath, $dstPath); } else { //problems with permissions on some systems? #rename($srcPath, $dstPath); self::copy($srcPath, $dstPath); self::remove($srcPath); } } public static function copy($srcPath, $dstPath) { if ($srcPath == $dstPath) throw new SimpleException('Trying to copy file to the same location'); copy($srcPath, $dstPath); } public static function remove($srcPath) { if (file_exists($srcPath)) unlink($srcPath); } public static function createDirectory($dirPath) { if (file_exists($dirPath)) { if (!is_dir($dirPath)) throw new SimpleException($dirPath . ' exists, but it\'s not a directory'); return; } mkdir($dirPath, 0777, true); } public static function handleUploadErrors($file) { switch ($file['error']) { case UPLOAD_ERR_OK: break; case UPLOAD_ERR_INI_SIZE: throw new SimpleException('File is too big (maximum size: %s)', ini_get('upload_max_filesize')); case UPLOAD_ERR_FORM_SIZE: throw new SimpleException('File is too big than it was allowed in HTML form'); case UPLOAD_ERR_PARTIAL: throw new SimpleException('File transfer was interrupted'); case UPLOAD_ERR_NO_FILE: throw new SimpleException('No file was uploaded'); case UPLOAD_ERR_NO_TMP_DIR: throw new SimpleException('Server misconfiguration error: missing temporary folder'); case UPLOAD_ERR_CANT_WRITE: throw new SimpleException('Server misconfiguration error: cannot write to disk'); case UPLOAD_ERR_EXTENSION: throw new SimpleException('Server misconfiguration error: upload was canceled by an extension'); default: throw new SimpleException('Generic file upload error (id: ' . $file['error'] . ')'); } if (!is_uploaded_file($file['tmp_name'])) throw new SimpleException('Generic file upload error'); } }