How to save excel document with Russian name phpexcel?

Question:

Hello to all! I use PHP, PHPExcel.

I am writing a script that takes the name of a table from the database and saves an excel document with its name. The name of the table in Russian.

$objWriter->save('teachears/РусскиеБУКВЫ.xlsx');//вместо РусскиеБуквы - переменная хранящая название таблицы.

As a result, the file has incomprehensible hieroglyphs in its name.

Is it possible to save a file with a Russian name? Or do you have to write a crutch to translate into English?


Added:

$connection = new mysqli("localhost", "admin", "1234", "teachers");
$connection->set_charset("utf8");
$query1 = "SHOW TABLES FROM `teachers`";
$result = mysqli_query($connection, $query1);

while ($row1 = mysqli_fetch_array($result, MYSQL_NUM)) {
$objPHPExcel->getActiveSheet()->setTitle('Демо');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', 'Мир!');
...Код наполнения файла данными...
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            $objWriter->save('teachears/'.$row1[0].'.xlsx');//где $row1[0] - название таблицы
}

Actually, the contents of the file are in order, the problem is only in the name.

Answer:

To summarize the comments:

When creating files in php, consider the file name encoding supported by the file system and operating system functions.

For example, for Windows (it can be seen that it is in this environment that the topstarter script is launched) the php script must use the cp1251 encoding, since files in php are created by the API function CreateFileA (at least it is in the php5ts.dll import list).

The problem indicated in the question is solved by the line $sysFilename = iconv('UTF-8', 'CP1251//IGNORE', $dbFilename); (as artoodetoo correctly suggested).

Additionally, you may need to filter file names for the content of prohibited characters (for Windows, these are <>:"/\|?* And" null "character), as well as trimming to the allowed length (for Windows – 255 characters) see Naming Conventions

PS. I checked it just in case. On Windows, you need to check the length of the path. The file is not created from php if the path length is more than 255 (which is strange, the documentation says about 260 characters)

Scroll to Top