Select files from a directory by name

0

I have the following code snippet that looks for files in the directory:

<?php session_start(); include("../conexao.php"); if(isset($_SESSION['MSLogin']) and isset($_SESSION['MSSenha']) ){} else {header("Location: index.php"); exit; } 

$QueryClient = mysql_query("SELECT * FROM clientes WHERE id = '".$_GET["CodigoCliente"]."'");
$Cliente = mysql_fetch_array($QueryClient);
$id = mysql_query("SELECT id FROM clientes WHERE id = '".$_GET["CodigoCliente"]."'");
$IDD = mysql_fetch_array($id);

?>


<h2> Imagens de <? echo $Cliente['nome'];?> </h2>

<style>
body {
    background: #FFFFFF;
}
</style>

<script type="text/javascript" src="../FANCYBOX/lib/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
    jQuery('#FormClient').submit(function(){
        var dados=jQuery(this).serialize();
        jQuery('#ResultClientEdit').html("<p align='center'><img src='../FANCYBOX/source/22.GIF'></p>");
        jQuery.ajax({
            type:"POST",
            url:"../administrativo/PC_processar_edicao_clientes.php",
            data:dados,
            success:function(data){$('#ResultClientEdit').html(data)}
            });
            return false
    })
});
</script>
<?php
$pasta = '../uploads';
if (is_dir($pasta)){

    $diretorio = dir($pasta);

    while(($arquivo = $diretorio->read()) !== false)

    {
        echo '<a target="_blanc" href='.$pasta."/".$arquivo.'>'.$arquivo.'</a><br />';

    }
    $diretorio->close();
}else{
    echo 'A pasta não existe.';
}

?>

What's the problem? This search returns all the files in the directory, but I would like to select them by name, since this name always starts with the client id. I hope I was clear, Thanks !!

    
asked by anonymous 25.07.2018 / 13:54

1 answer

0

If all the files are in the same folder, your mistake is to list the files without doing a file check by the user ID. You can solve by putting a check inside while as follows:

// a variável $pattern pode ser colocada antes do while
$pattern = '/' . $idDoClienteAqui . '/';

// verifica se o nome do arquivo contém o ID do usuário
// IF dentro do WHILE
if (preg_match($pattern, $arquivo)) {
  // listar arquivo
}

Test performed:

$arquivo = '123-Thiago';
$idDoClienteAqui = '123';

// a variável $pattern pode ser colocada antes do while
$pattern = '/' . $idDoClienteAqui . '/';

// verifica se o nome do arquivo contém o ID do usuário
// IF dentro do WHILE
if (preg_match($pattern, $arquivo)) {
  // listar arquivo
    echo "entrou";
}
    
25.07.2018 / 14:06