I'm currently building a custom module in Drupal. I'm trying to get some file information (for example file name, date modified or uploaded, etc) and then display this information in a table.
The files are all PDF and are being uploaded via FTP.
Here are some links that I was using for reference:
Getting the names of all files in a directory with PHP
List all files in one directory PHP
I'm using the glob() and I double checked to see that my path is correct however I keep getting an empty array. Below is my code, I stripped out most of the information that's not necessary for this question. I have 3 different attempts in the code, but I'm not running all 3 together.
<?php
namespace Drupal\Documents\Controller;
use Drupal\Core\Controller\ControllerBase;
class DocumentController extends ControllerBase {
public function documents(){
global $base_url;
$testPath = $base_url . '/sites/default/files/documents/';
//Attempt One
$newPath = $base_url . '/sites/default/files/documents/*.*';
foreach(glob($newPath) as $file) {
print_r($file);
print_r('test');
}
//Attempt 2
$log_directory = $base_url . '/sites/default/files/documents';
print_r($log_directory);
$results_array = array();
if (is_dir($log_directory))
{
if ($handle = opendir($log_directory))
{
//Notice the parentheses I added:
while(($file = readdir($handle)) !== FALSE)
{
$results_array[] = $file;
print_r('Testing');
}
closedir($handle);
}
}
//Output findings
foreach($results_array as $value)
{
echo $value . '<br />';
}
//Attempt 3
foreach (array_filter(glob($base_url . '/sites/default/files/documents/*'), 'is_file') as $file)
{
echo 'Test';
}
}
}
When I print_r() $testPath here is what I get, so path looks correct: http://localhost/drupaldev/sites/default/files/documents/