Problem writing files with special characters in PHP + Apache - encoding problem

again, I'm having encoding problems when trying to pin files in a PHP application.

Here is the deal, whenever the file name contains special characters, that is: "eñeìá.html", I cannot pin it correctly .. the result of fastening it with the php AddFile function is "e + | e + ¼ + í. html

The problem line is as follows:

$ zip-> addFile ($ file_to_add_path, $ file_to_add-> getFilename ());

I already tried using iconv, utf8_decode / encode, etc., but so far no luck. The closer I got, the higher the example when using htmlentities, and then decrypted them.

I run the application in Xampp on Win XP .. This may be the root of the problem.

It's funny when I unzip a file with the name specified earlier in the application, its name is fine, however, when I download a zip file and open it ... bleh ..

In any case, thanks in advance to everyone who could help me or help me a little with this. If you need more information, please ask me about it.

Regards

+3
source share
5 answers

I had the same problem with Central European characters, solved with help iconv("UTF-8", "CP852", $string); where CP852 is the old DOS encoding for Central Europe. Thus, this can help to use the appropriate encoding for your language (I think this is determined by the internal configuration of the ZIP algorithm or something else).

+4
source

, , winRAR - ? , . , , , , unicode, , , .

0

iconv out_charset? , "los niños.txt", "los nios.txt"

<?php
$archivePath = realpath('.\test.zip');
$archive = new ZipArchive;
$opened = $archive->open($archivePath, ZIPARCHIVE::OVERWRITE);
if ($opened === true) {
    $directory = new DirectoryIterator('.');
    foreach($directory as $fileInfo) {
        if ($fileInfo->isDot()) {
            continue;
        }

        if (preg_match('#.*\.txt$#', $fileInfo->getBasename())) {
            $cleanFilename = iconv("UTF-8", "ISO-8859-1//IGNORE", $fileInfo->getFilename());
            $archive->addFile($fileInfo->getRealPath(), $cleanFilename);
        }
    }
    $closed = $archive->close();
    if (!$closed) {
        echo "Could not create ZIP file<br/>";
    }
} else {
    echo "Could not open archive because of code {$opened}<br/>";
}

, iconv UTF-8, .

0

$clean_filename = iconv("ISO-8859-1", "CP860", $filename);

To fix my problem with Portuguese file names, change the CP860 to match the code that catches your special characters best.

https://en.wikipedia.org/wiki/Code_page

0
source

Source: https://habr.com/ru/post/1782627/


All Articles