List files in a folder containing a specific word in the name

Asked

Viewed 993 times

1

I’m developing a system in Joomla! and I need to provide specific files for each user (holerite). I have already managed through the session to know which user is logged in. In the Joomla! , I added an extra field in the database to enter a user registration number, this number is what differentiates the names of the holerite files, would like to make in php a page that lists the files within a directory that contains the user-specific registration number.

Ex.: One of the files has this format

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

When logging in user 1, his code is 000956 which I have already managed to get through the session. Then I would like to display only its corresponding file, for each user only to see its holerite. Does anyone have any idea how I can do this in PHP?

Incidentally this is the code I used in PHP to pick up 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

?>

3 answers

4

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

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

In the $output_array, you will have a code_user index, compare with what you have and do ;)

  • https://regex101.com/r/bpyV2O/1

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 on the hard side unnecessarily. This solution depends on you deleting from the directory the files that are no longer available and this is a complicator.

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

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

  • Really the ideal would be to put this information directly in the database more, in the case of this system that I will develop is a system of holerite every month the file will change or add a new and are more than 400 users, it would be arduous to upload each file stating which corresponding user, because who will do this is RH. What I really wanted was, to put every month the holerite files a folder (can stay several months since it is the same user) and in php I display this directory for download but filtering by the user. My Php knowledge is low

  • In this case this solution solves you. I just thought I’d put the warning to whoever else is looking.

0

I kept thinking about what had told me about resources so I decided to change my code to something simpler and it was like this, not yet finished but it gives to have an idea of how it will be

<?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";
}
?>

Browser other questions tagged

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