Listing directories "Client" with PHP - Error when directory name has accent

Asked

Viewed 127 times

1

Good morning. I’ve been trying for at least two days to find a solution to the problem I’m facing. I’m still a beginner in php and that’s why so much difficulty...

Well, here’s the problem: I want to create a web application to use here in the company’s IT. This application consists of navigating through a folder structure that I will make available on the network. The ultimate goal is to get the folder indicated by the user. "But why is that?" one might ask. So the answer is: The user needs to fill out an electronic form and one of the fields in this form consists of the user specifying the full path of the folder where he wants access. So that the user does not need to copy the path through the Explorer (windows), he would do it directly by the application,

Well, I managed to implement the navigation and I ended up bumping into a problem: folders with names like "my folder is pure action". IE, folder with accentuation.

When I pass this kind of path I get the error: "The system n?o can find the specified file. (code: 2) in C: xampp htdocs test requisicao.php on line 44"

I’d like someone to help me fix this problem.

Here’s the code for anyone who can help: index php.:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Ajax com jQuery</title>
    <style type="text/css"> #box {border:1px solid #ccc; padding:5px} </style>
</head>
<body>
    <legend>Setor desejado</legend>
    <select id="select_setor" onchange="">
        <option id="null" value="null">Selecione o Setor</option>
        <option id="adm" value="adm">Administrativo</option>
        <option id="com" value="com">Comercial</option>
        <option id="dir" value="dir">Diretoria</option>
        <option id="ope" value="ope">Operacional</option>
        <option id="pro" value="pro">Produtivo</option>
        <option id="qua" value="qua">Qualidade</option>
    </select>
    <div>
        <legend id="raiz">Diretorio: </legend>
        <div id="dir_setor"></div>
    </div>    
    <div>
        <legend>Escolhidos (form com ajax)</legend>
        <div id="box"></div>
    </div>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#select_setor").change(function(event) {
            $("#null").attr('disabled','disabled');
            var setor = $("#select_setor").val();
            setLegenda();
            $("#dir_setor").load('requisicao.php',{setor:setor},ready());
        });
    });
    function getDir(e) {
        var name = e.name;
        var subs = '//10.40.0.200/Diretorios/Setores/';
        var legenda = "Diretorio: " + name.replace(subs, '');
        document.getElementById('raiz').innerHTML = legenda;
        $("#dir_setor").load('requisicao.php',{diretorio:name},ready());
    }
    function setLegenda(){
        var subs = '//10.40.0.200/Diretorios/Setores/';
        var selected = $('#select_setor :selected').text();
        var legenda = "Diretorio: " + selected.replace(subs, '');
        document.getElementById('raiz').innerHTML = legenda;
    }
    function ready(){}
</script></html>

And this is the requisicao.php:

<?php
ini_set( 'default_charset', 'utf-8');
$setor = (array_key_exists('setor', $_POST) ? $_POST['setor'] : null);
$diretorio = (array_key_exists('diretorio', $_POST) ? $_POST['diretorio'] : null);    
$dirNome = $diretorio;  
if($setor == null){
    $setor = '//10.40.0.200/Diretorios/Setores/';
    $abreDir = $diretorio;
}else{
    $diretorio = '//10.40.0.200/Diretorios/Setores/';
    switch($setor){        
        case 'adm':
            $setor = '//10.40.0.200/Diretorios/Setores/Administrativo/';
        break;

        case 'com':
            $setor = '//10.40.0.200/Diretorios/Setores/Comercial/';   
        break;

        case 'dir':
            $setor = '//10.40.0.200/Diretorios/Setores/Diretoria/';  
        break;

        case 'ope':
            $setor = '//10.40.0.200/Diretorios/Setores/Operacional/'; 
        break;

        case 'pro':
            $setor = '//10.40.0.200/Diretorios/Setores/Produtivo/';
        break;

        case 'qua':
            $setor = '//10.40.0.200/Diretorios/Setores/Qualidade/'; 
        break;        
    }
    $abreDir = $setor;
 }  
$openDir = dir($abreDir);
$tempDir = explode('/', $dirNome);
$backDir = $abreDir;
$indice = count($tempDir) - 2;
if($indice > 2){ $backDir = str_replace($tempDir[$indice].'/', "", $dirNome); }
$table = '';
$table .= '<table id="table_dir" style="border=1px ">';
    while ($arq = $openDir -> read()) {
        if($arq != '.' && $arq != '..'){
            if(is_dir($abreDir.$arq)){
                $arq =      utf8_encode($arq);
                $abreDir =  utf8_encode($abreDir);
                $table .= '<tr>';
                $table .= '<td>';
                $table .= $arq;
                $table .= '</td>';
                $table .= '<td><a name="';
                $table .= $abreDir.$arq;
                $table .= '/" href="#" class"href_dir" onclick="getDir(this)"> Abrir </a></td>';

                $table .= '</tr>';
            }else{
                if(strpos('~$', $arq)){
                    echo '<tr>';    
                    echo '<td>'.$arq.'</td>';
                    echo '<td> -- </td>';
                    echo '</tr>';
                }

            }
        }
    }
$table .= '</table>';
echo $table;
if ($abreDir != $setor) { echo '<td><a name="'.$backDir.'" href="#" class"href_dir" onclick="getDir(this)"> Voltar </a></td>'; }
$openDir->close(); ?>

1 answer

0

To solve the problem I changed:

$openDir = dir($abreDir);

To:

$openDir = dir(utf8_decode($abreDir));

Sorry I even answer my question, but is that from what I read in the "help" of stackoverflow I need to vote on an answer for my question to be answered.

Browser other questions tagged

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