Documentation

SaveForDB

bool
CFile::SaveForDB(
 array &Fields, 
 string field,
string SavePath
);

The method is used to prepare data prior to inserting/updating a record in the data base, containing link to file. The method saves file to disk and returns the ID of the saved file via the input array to further insert/update the record in the data base. Static method.

Note: prior to version 7.1.0 the same actions were performed by the CDatabase::PrepareInsert and CDatabse::PrepareUpdate methods. Starting from version 7.1.0 the actions to save files and insertions into the data base are located between different methods.

Parameters

ParameterDescription
arFields

Array of field values in the format "field_name1"=>"value1", "field_name2"=>"value2" [, ...].
When file attached to the record is inserted, the "value" must be presented as an array
Array(
"name" => "file name",
"size" => "size",
"tmp_name" => "temporary path to server ",
"type" => "type of uploaded file",
  "del" => "flag, specifies to delete an existing file (non-empty string)",  
"MODULE_ID" => "module name");

Array of this type can be obtained, for example, by joining the arrays $HTTP_POST_FILES[field_name] and Array("del" => ${"field_name"."_del"}, "MODULE_ID" = "module_name");

The value is passed via the link.

fieldField name in the arFields array that contains the file.
SavePathFolder name (will be located inside the 'upload' folder) for file storage.
Prior to version 12.0.8 the parameter was called dir.

Returned value

True on success, false otherwise. The $arFields[$field] array element value will return the ID of the saved file from the "b_file" registrar table. If the file was deleted, false will be returned in this field. In case of failure, the element of the $arFields[$field] array will be deleted (unset).

Examples of use

<?
$arFields["ATTACH_IMG"] = $_FILE["ATTACH_IMG"];
$arFields["ATTACH_IMG"]["MODULE_ID"] = "blog";

CFile::SaveForDB($arFields, "ATTACH_IMG", "blog");
$arInsert = $DB->PrepareInsert("b_blog_post", $arFields);

?>


© «Bitrix24», 2001-2024
Up