Accentuation of files in directory reading

Asked

Viewed 1,713 times

3

I created a simple reading of directories in PHP, but when I send files with accentuation, they appear with strange characters and then I cannot open these files.

How to read the accent correctly?

I’ve tried a lot of things changing to UTF8 in HTML, but so far nothing.

 <?php
    //diretorio
    $base = 'files/';
    $abreDir = ($_GET['dir'] != '' ? $_GET['dir'] : $base);
    $openDir = dir($abreDir);            
    //voltar
    $strrdir = strrpos(substr($abreDir,0,-1),'/');
    $voltar = substr($abreDir,0,$strrdir+1);

    $openDir = dir($abreDir);                 
       while($arq = $openDir -> read()):
            if($arq != '.' && $arq != '..'):
                if(is_dir($abreDir.$arq)):
                    //pastas                                                        
                    echo'<li class="folders"><a href="discovirtual?dir='.$abreDir.$arq.'/">'.$arq.'</a></li>';          
                else:
                    //arquivos     
                    echo'<li class="files"><a href="'.$abreDir.$arq.'">'.$arq.'</a></li>';                              
                endif;                      
            endif;              
       endwhile;
    if($abreDir != $base):
        echo '<a href="discovirtual?dir='. $voltar.'">Voltar</a>';
    endif;
    $openDir -> close();
?>
  • Is it the listing or the time you click on the link to open a file? In the latter case, the file is being server directly by your webserver (apache?), and you need to configure the default encoding to serve each file type. Even so it may not help, since there may be on disk files of the same type with different encodings...

  • First of all, thank you for answering. When I rename it with accentuation, it passes the file to be opened with accentuation in the browser, so I cannot view the file. And when I send the file accentuated to the server, at the time I see in my listing it displays me with strange characters like triangles and etc... Friends I don’t know how to insert the decodes... as I would put utf8_decode in the variable, has any hint... Thanks

1 answer

2


There are a few ways to solve it. The one I most recommend is renomear os arquivos with a hash in sha1 or remove accents at the time of inclusion. This will solve problems with accents and special characters.

Another way to convert these characters you quoted is by using utf8_decode(); or utf8_encode();

What do they do?

utf8_encode - Encodes an ISO-8859-1 string for UTF-8

utf8_decode - Converts a string with ISO-8859-1 characters UTF-8 coded for single-byte ISO-8859-1.

  • First of all, thank you for answering. When I rename it with accentuation, it passes the file to be opened with accentuation in the browser, so I cannot view the file. And when I send the file accentuated to the server, at the time I see in my listing it displays me with strange characters like triangles and etc... Friends I don’t know how to insert the decodes... as I would put utf8_decode in the variable, has any hint... Thanks

  • Normally I go with two feet on the chest of anyone who suggests the use of utf8_encode() / utf8_decode() (laughs), but in this particular case, it is really the best, not to say single, output, since even the so promised PHP 6, PHP does not support Unicode operations.

  • I have an idea, I will create a function to remove accents and higher characters and replace spaces with strokes like a friendly url... let’s see if I can. Once I have an answer put here the result. Thank you for the answers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.