• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/general/file.php
  • Класс: CFile
  • Вызов: CFile::CopyFile
static function CopyFile($FILE_ID, $bRegister = true, $newPath = "")
{
	$z = static::GetByID($FILE_ID);
	if($zr = $z->Fetch())
	{
		/****************************** QUOTA ******************************/
		if (COption::GetOptionInt("main", "disk_space") > 0)
		{
			$quota = new CDiskQuota();
			if (!$quota->checkDiskQuota($zr))
				return false;
		}
		/****************************** QUOTA ******************************/

		$strNewFile = '';
		$bSaved = false;
		$bExternalStorage = false;
		foreach(GetModuleEvents("main", "OnFileCopy", true) as $arEvent)
		{
			if($bSaved = ExecuteModuleEventEx($arEvent, array(&$zr, $newPath)))
			{
				$bExternalStorage = true;
				break;
			}
		}

		$io = CBXVirtualIo::GetInstance();

		if(!$bExternalStorage)
		{
			$strDirName = $_SERVER["DOCUMENT_ROOT"]."/".(COption::GetOptionString("main", "upload_dir", "upload"));
			$strDirName = rtrim(str_replace("//","/",$strDirName), "/");

			$zr["SUBDIR"] = trim($zr["SUBDIR"], "/");
			$zr["FILE_NAME"] = ltrim($zr["FILE_NAME"], "/");

			$strOldFile = $strDirName."/".$zr["SUBDIR"]."/".$zr["FILE_NAME"];

			if($newPath <> '')
			{
				$strNewFile = $strDirName."/".ltrim($newPath, "/");
			}
			else
			{
				$strNewFile = $strDirName."/".$zr["SUBDIR"]."/".md5(uniqid(mt_rand())).strrchr($zr["FILE_NAME"], ".");
			}

			$zr["FILE_NAME"] = bx_basename($strNewFile);
			$zr["SUBDIR"] = mb_substr($strNewFile, mb_strlen($strDirName) + 1, -(mb_strlen(bx_basename($strNewFile)) + 1));

			if($newPath <> '')
			{
				CheckDirPath($strNewFile);
			}

			$bSaved = copy($io->GetPhysicalName($strOldFile), $io->GetPhysicalName($strNewFile));
		}

		if($bSaved)
		{
			if($bRegister)
			{
				$NEW_FILE_ID = static::DoInsert($zr);

				if (COption::GetOptionInt("main", "disk_space") > 0)
					CDiskQuota::updateDiskQuota("file", $zr["FILE_SIZE"], "copy");

				static::CleanCache($NEW_FILE_ID);

				return $NEW_FILE_ID;
			}
			else
			{
				if(!$bExternalStorage)
					return mb_substr($strNewFile, mb_strlen(rtrim($_SERVER["DOCUMENT_ROOT"], "/")));
				else
					return $bSaved;
			}
		}
		else
		{
			return false;
		}
	}
	return 0;
}