Example #1
0
 /**
  * <p>Метод удаляет файл из таблицы зарегистрированных файлов (b_file) и с диска. Статичный метод.</p>
  *
  *
  * @param int $id  Цифровой идентификатор файла.
  *
  * @return mixed 
  *
  * <h4>Example</h4> 
  * <pre>
  * &lt;?
  * // удаляем изображение формы
  * $arFilter = array("ID" =&gt; 1, "ID_EXACT_MATCH" =&gt; "Y");
  * $rsForm = CForm::GetList($by, $order, $arFilter, $is_filtered);
  * if ($arForm = $rsForm-&gt;Fetch())
  * {
  *     if (intval($arForm["IMAGE_ID"])&gt;0) <b>CFile::Delete</b>($arForm["IMAGE_ID"]);	
  * }
  * ?&gt;
  * </pre>
  *
  *
  * <h4>See Also</h4> 
  * <ul> <li> <a href="http://dev.1c-bitrix.ru/api_help/main/functions/file/deletedirfiles.php">DeleteDirFiles</a> </li>
  * <li> <a href="http://dev.1c-bitrix.ru/api_help/main/functions/file/deletedirfilesex.php">DeleteDirFilesEx</a> </li>
  * </ul><a name="examples"></a>
  *
  *
  * @static
  * @link http://dev.1c-bitrix.ru/api_help/main/reference/cfile/delete.php
  * @author Bitrix
  */
 public static function Delete($ID)
 {
     global $DB;
     $io = CBXVirtualIo::GetInstance();
     $ID = intval($ID);
     if ($ID <= 0) {
         return;
     }
     $res = CFile::GetByID($ID);
     if ($res = $res->Fetch()) {
         $delete_size = 0;
         $upload_dir = COption::GetOptionString("main", "upload_dir", "upload");
         $dname = $_SERVER["DOCUMENT_ROOT"] . "/" . $upload_dir . "/" . $res["SUBDIR"];
         $fname = $dname . "/" . $res["FILE_NAME"];
         $file = $io->GetFile($fname);
         if ($file->isExists() && $file->unlink()) {
             $delete_size += $res["FILE_SIZE"];
         }
         $delete_size += CFile::ResizeImageDelete($res);
         $DB->Query("DELETE FROM b_file WHERE ID = " . $ID);
         $directory = $io->GetDirectory($dname);
         if ($directory->isExists() && $directory->isEmpty()) {
             $directory->rmdir();
         }
         CFile::CleanCache($ID);
         foreach (GetModuleEvents("main", "OnFileDelete", true) as $arEvent) {
             ExecuteModuleEventEx($arEvent, array($res));
         }
         /****************************** QUOTA ******************************/
         if ($delete_size > 0 && COption::GetOptionInt("main", "disk_space") > 0) {
             CDiskQuota::updateDiskQuota("file", $delete_size, "delete");
         }
         /****************************** QUOTA ******************************/
     }
 }
Example #2
0
 function UpdateDesc($ID, $desc)
 {
     global $DB;
     $DB->Query("UPDATE b_file SET DESCRIPTION='" . $DB->ForSql($desc, 255) . "' WHERE ID=" . intval($ID));
     CFile::CleanCache($ID);
 }
Example #3
0
 /**
  * @param array[string]string $arFile
  * @return void
  */
 public static function FixFileContentType(&$arFile)
 {
     global $DB;
     $fixedContentType = "";
     if ($arFile["CONTENT_TYPE"] === "image/jpg") {
         $fixedContentType = "image/jpeg";
     } else {
         $hexContentType = unpack("H*", $arFile["CONTENT_TYPE"]);
         if ($hexContentType[1] === "e0f3e4e8ee2f6d706567" || $hexContentType[1] === "d0b0d183d0b4d0b8d0be2f6d706567") {
             $fixedContentType = "audio/mpeg";
         }
     }
     if ($fixedContentType !== "") {
         $arFile["CONTENT_TYPE"] = $fixedContentType;
         $DB->Query("\n\t\t\t\tUPDATE b_file\n\t\t\t\tSET CONTENT_TYPE = '" . $DB->ForSQL($fixedContentType) . "'\n\t\t\t\tWHERE ID = " . intval($arFile["ID"]) . "\n\t\t\t");
         CFile::CleanCache($arFile["ID"]);
     }
 }
Example #4
0
 function SaveFile($arFile, $path)
 {
     $err_mess = CFile::err_mess() . "<br>Function: SaveFile<br>Line: ";
     global $DB;
     $strFileName = basename($arFile["name"]);
     $extension = strrchr($arFile["name"], ".");
     $upload_dir = COption::GetOptionString("main", "upload_dir", "upload");
     if (strlen($arFile["del"]) > 0 && strlen($strFileName) <= 0) {
         return "NULL";
     }
     if (is_set($arFile, "content") && !is_set($arFile, "size")) {
         $arFile["size"] = strlen($arFile["content"]);
     }
     if (COption::GetOptionString("main", "save_original_file_name", "N") == "Y") {
         if (COption::GetOptionString("main", "convert_original_file_name", "Y") == "Y") {
             $strFileName = preg_replace('/([^' . BX_VALID_FILENAME_SYMBOLS . '])/e', "chr(rand(97, 122))", $strFileName);
         }
         $dir_add = "";
         $i = 0;
         while (true) {
             $dir_add = substr(md5(uniqid(mt_rand(), true)), 0, 3);
             if (!file_exists($_SERVER["DOCUMENT_ROOT"] . "/" . $upload_dir . "/" . $path . "/" . $dir_add . "/" . $strFileName)) {
                 break;
             }
             if ($i >= 25) {
                 $dir_add = md5(uniqid(mt_rand(), true));
                 break;
             }
             $i++;
         }
         if (substr($path, -1, 1) != "/") {
             $path .= "/" . $dir_add;
         } else {
             $path .= $dir_add . "/";
         }
         $new_file = $strFileName;
     } else {
         $new_file = md5(uniqid(mt_rand(), true)) . $extension;
         if (substr($path, -1, 1) != "/") {
             $path .= "/" . substr($new_file, 0, 3);
         } else {
             $path .= substr($new_file, 0, 3) . "/";
         }
     }
     if (strlen($arFile["name"]) <= 0 || intval($arFile["size"]) <= 0 || strlen($arFile["type"]) <= 0) {
         $old_id = intval($arFile["old_file"]);
         if (is_set($arFile, "description") && $old_id > 0) {
             $strSql = "\n\t\t\t\t\tUPDATE b_file SET\n\t\t\t\t\t\tDESCRIPTION = '" . $DB->ForSQL($arFile["description"], 255) . "'\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tID = {$old_id}\n\t\t\t\t\t";
             $DB->Query($strSql, false, $err_mess . __LINE__);
             CFile::CleanCache($old_id);
         }
         return false;
     }
     $dir = $_SERVER["DOCUMENT_ROOT"] . "/" . $upload_dir . "/" . $path . "/";
     CheckDirPath($dir);
     $strDbFileNameX = $dir . $new_file;
     if (is_set($arFile, "content")) {
         $f = @fopen($strDbFileNameX, "ab");
         if (!$f) {
             return false;
         }
         if (!fwrite($f, $arFile["content"])) {
             return false;
         }
         fclose($f);
     } elseif (!@copy($arFile["tmp_name"], $strDbFileNameX)) {
         return false;
     }
     @chmod($strDbFileNameX, BX_FILE_PERMISSIONS);
     $arImage = @getimagesize($strDbFileNameX);
     if (is_array($arImage)) {
         $width = $arImage[0];
         $height = $arImage[1];
     } else {
         $width = 0;
         $height = 0;
     }
     if ($arFile["type"] == "image/pjpeg") {
         $arFile["type"] = "image/jpeg";
     }
     $strSql = "\n\t\t\tINSERT INTO b_file (\n\t\t\t\tHEIGHT,\n\t\t\t\tWIDTH,\n\t\t\t\tFILE_SIZE,\n\t\t\t\tCONTENT_TYPE,\n\t\t\t\tSUBDIR,\n\t\t\t\tFILE_NAME,\n\t\t\t\tMODULE_ID,\n\t\t\t\tORIGINAL_NAME,\n\t\t\t\tDESCRIPTION\n\t\t\t) VALUES (\n\t\t\t\t'" . $height . "',\n\t\t\t\t'" . $width . "',\n\t\t\t\t'" . intval($arFile["size"]) . "',\n\t\t\t\t'" . $DB->ForSql($arFile["type"], 255) . "' ,\n\t\t\t\t'" . $DB->ForSql($path, 255) . "',\n\t\t\t\t'" . $DB->ForSql($new_file, 255) . "',\n\t\t\t\t'" . $arFile["MODULE_ID"] . "',\n\t\t\t\t'" . $DB->ForSql($strFileName, 255) . "',\n\t\t\t\t'" . $DB->ForSql($arFile["description"], 255) . "'\n\t\t\t)\n\t\t\t";
     //echo "<pre>".$strSql."</pre>";
     $DB->Query($strSql, false, $err_mess . __LINE__);
     $NEW_FILE_ID = $DB->LastID();
     CFile::CleanCache($NEW_FILE_ID);
     return $NEW_FILE_ID;
 }
Example #5
0
 function UpdateExternalId($ID, $external_id)
 {
     global $DB;
     $external_id = trim($external_id);
     $DB->Query("UPDATE b_file SET EXTERNAL_ID=" . ($external_id != "" ? "'" . $DB->ForSql($external_id, 50) . "'" : "null") . " WHERE ID=" . intval($ID));
     CFile::CleanCache($ID);
 }
     }
     if (isset($_SESSION["arMoveStat_skip"])) {
         $_skip = intval($_SESSION["arMoveStat_skip"]);
     }
 }
 $files_per_step = 50;
 $rsNextFile = $DB->Query($DB->TopSQL("\n\t\t\t\t\tSELECT *\n\t\t\t\t\tFROM b_file\n\t\t\t\t\tWHERE ID > " . intval($ob->LAST_FILE_ID) . "\n\t\t\t\t\tAND (HANDLER_ID IS NULL OR HANDLER_ID <> '" . $DB->ForSQL($ob->ID) . "')\n\t\t\t\t\tORDER BY ID ASC\n\t\t\t\t", $files_per_step));
 $counter = 0;
 $bWasMoved = false;
 $moveResult = CCloudStorage::FILE_SKIPPED;
 while ($moveResult == CCloudStorage::FILE_PARTLY_UPLOADED || is_array($arFile = $rsNextFile->Fetch())) {
     CCloudStorage::FixFileContentType($arFile);
     $moveResult = CCloudStorage::MoveFile($arFile, $ob);
     if ($moveResult == CCloudStorage::FILE_MOVED) {
         $DB->Query("\n\t\t\t\t\t\t\tUPDATE b_file\n\t\t\t\t\t\t\tSET HANDLER_ID = '" . $DB->ForSQL($ob->ID) . "'\n\t\t\t\t\t\t\tWHERE ID = " . intval($arFile["ID"]) . "\n\t\t\t\t\t\t");
         CFile::CleanCache($arFile["ID"]);
         $_done += 1;
         $_size += doubleval($arFile["FILE_SIZE"]);
         $bWasMoved = true;
         $ob->Update(array("LAST_FILE_ID" => $arFile["ID"]));
         $counter++;
     } elseif ($moveResult == CCloudStorage::FILE_SKIPPED) {
         $e = $APPLICATION->GetException();
         if (is_object($e)) {
             $message = new CAdminMessage(GetMessage("CLO_STORAGE_LIST_MOVE_FILE_ERROR"), $e);
             break;
         } else {
             $_skip += 1;
             $ob->Update(array("LAST_FILE_ID" => $arFile["ID"]));
             $counter++;
         }