Рецепты и примеры кода для CMS Bitrix

Переименовать файлы


$rs = \Bitrix\Main\FileTable::getList([
	'filter' => ['MODULE_ID' => 'main', '%FILE_NAME' => ' '],
	'select' => ['ID', 'SUBDIR', 'FILE_NAME', 'DESCRIPTION'],
	'limit' => 10
]);
$dir = $_SERVER['DOCUMENT_ROOT'].'/upload/';
$connection = \Bitrix\Main\Application::getConnection();
while($row = $rs->fetch()){
	$file = $dir.$row['SUBDIR'].'/'.$row['FILE_NAME'];
	if(file_exists($file)){
		$newFileName = str_replace(' ', '_', $row['FILE_NAME']);
		$toFile = $dir.$row['SUBDIR'].'/'.$newFileName;
		var_dump($row);
		var_dump(rename($file,$toFile));
		$connection->query('UPDATE b_file SET FILE_NAME = "'.$newFileName.'" WHERE ID = '.$row['ID']);
	}
}