List files in a folder that contain a specific word in the name

1

I am developing a system in Joomla! and need to make specific files available to each user (holerite). I already managed through the session to know which user is logged in. In Joomla !, I added an extra field in the database to insert a user's registration number, this number is what differentiates the names of the holerite files, I would like to make in php a page that lists the files inside of a directory that contains the user-specific registry number.

Eg one of the files has this format

holerith 000956  Dezembro de 2016.pdf
holerith 000957  Dezembro de 2016.pdf

When I log on to user 1, his code is 000956 that I have already been able to get through the session. I would then like to display only the corresponding file of it, for each user only to see your netbook. Does anyone have any idea how I can do this in PHP?

Incidentally this is the code I used in PHP to get the Joomla session which user is logged in.

<?php
// Recuperando Sessão do usuário Joomla
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
$path = "\xampp\htdocs\TESTES\Joomla_Teste";  //caminho da instalação do Joomla
define('JPATH_BASE', $path);
require_once JPATH_BASE . DS . 'includes' . DS . 'defines.php';
require_once JPATH_BASE . DS . 'includes' . DS . 'framework.php';

$mainframe =& JFactory::getApplication('site');
$db = &JFactory::getDBO();
$mainframe->initialise();
$user =& JFactory::getUser( );


echo $user->id;  //imprime id do usuário
echo $user->name; //imprime nome do usuário
echo $user->password; // Imprime senha do usuário
echo $user->cod_func; // imprime o código do usuário

?>
    
asked by anonymous 07.11.2016 / 05:46

3 answers

4

The ideal would be to test with preg_match if the file contains what you need. Go through the files and with the string of the name of each one, it does something like that.

<?php
$encontrou = preg_match("/(?:holerith)(?:.+?)(?<codigo_usuario>\d+)/", $string_com_arquivo, $output_array);
?>

In the $ output_array, it will have an index of user_code, compare with what you have and done;)

    
07.11.2016 / 12:13
1

To list files in one or more folders you can use glob()

// codigo do usuário
$codigo = '1234';
// arquivos .pdf na pasta /path/ começados com holerith com 1234 no nome.
$arquivos = glob('/path/holerith*'.$codigo.'*.pdf');

That said, maybe you're solving the problem the hard way unnecessarily. This solution depends on you deleting the files that are no longer available from the directory and this is a complication.

Normally when there is a variable volume of files to download per user, the common procedure is to register in the bank which of these files are available and to present this list to the user without having to read from the system.

This reduces resource consumption since you do not need to read filesystem every time a user loads the page. If you need confirmation that the file exists it can iterate through the list stored in the database and check each file with file_exists() . Consider if this idea does not solve your problem better.

    
07.11.2016 / 12:32
0

I was thinking about what he had told me about resources so I decided to change my code to something simpler and it was like this, it's not finished yet, but I already have an idea how it will look.

<?php
 function varSet($VAR) { return isset($_GET[$VAR]) ? $_GET[$VAR] : ""; }
 $action = varSet("action");
 $pasta = base64_decode(varSet('pasta'));

 //Lista dos arquivos que nao serão listados
 $denyFiles = array(".htaccess","thumbs.db");

 if ($action == "download") {
    $file = base64_decode(varSet("file"));
        header("Content-disposition: attachment; 
        filename=\"".basename($file)."\"");
        readfile(".$file");
    exit;
 }
?>

<?php
 $pasta = '/arquivos';
 $arquivos = "$user->cod_func".'  Outubro de 2016.pdf';
 $filename = 'arquivos/'.$arquivos;
 if (file_exists($filename)) {
?>
<a href="?action=download&file=<?php echo base64_encode("$pasta/$arquivos"); ?>"><?php echo $arquivos; ?></a>
<br>
<?php
} else {
 echo "Não existe holerith no mês selecionado";
}
?>
    
09.11.2016 / 00:55