Select files from a directory by name

Asked

Viewed 281 times

0

I have the following code snippet that searches 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 big deal? This search brings me back all the files from the directory You would like to select them by name, since that name always starts with the client id. I hope I’ve been clear, Thank you!!

1 answer

0

If all the files are in the same folder, your mistake is to list the files without doing a check of the file by the user ID. You can resolve by placing a check inside the 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";
}
  • It was bad guy, it could be that I’m still doing something wrong, but when I get this piece of code, it doesn’t bring any results, I made several modifications trying to make it work but it didn’t work. have another idea or suggestion ?

  • Put the pattern thus: $pattern = '/' . $_GET["CodigoCliente"] . '/'; ?

  • I put yes, but nothing returns

  • I showed you the test that I did. If it’s not working, I believe either the variable pattern is not receiving the string with the client code, or the variable $arquivo is not receiving the string that contains the code that is in $pattern.

Browser other questions tagged

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