1

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/

hisusu32
  • 437
  • 7
  • 22
  • Welcome to SO! What is the value of $base_url? – bestprogrammerintheworld Aug 01 '19 at 18:24
  • You can't link to localhost because that's your own computer/server and it is not accessible through internet (not that way anyway :-)) – bestprogrammerintheworld Aug 01 '19 at 18:24
  • the value of $base_url is http://localhost/drupaldev – hisusu32 Aug 01 '19 at 18:27
  • thank you and wow!! I didn't think about that, is there no way to do this on my local server? – hisusu32 Aug 01 '19 at 18:28
  • 1
    `glob` works on directories not URLs. You need the absolute path to where the files are to be able to use `glob`. – Dave Aug 01 '19 at 18:33
  • Hey Dave! Thank you, that helped!! I'm now retrieving the urls for each file, im using this "foreach(glob($newPath) as $file)" and I'm getting back the url for each file.. Is there a way to get back an array with more information for each file or does that not work? – hisusu32 Aug 01 '19 at 18:40
  • What I mean by that, is right now I'm using multiple functions to grab file name without extension, grab file modified date, and whatnot.. I was hoping there was another function with all this information accessible – hisusu32 Aug 01 '19 at 19:01
  • @hisusu32 - There is no such function out of the box (that I know of), but I think you would be able to use stat function (https://www.php.net/manual/en/function.stat.php) together with your glob-function :-) – bestprogrammerintheworld Aug 03 '19 at 18:13
  • @hisusu32 - if someone answered your question (@Dave) please tell him to put that as an answer so this question could be marked as solved :-) – bestprogrammerintheworld Aug 03 '19 at 18:29
  • @ Dave and/or @bestprogrammerintheworld both of you gave me the correct answer. If either of you can respond to the question, I can mark as solved! – hisusu32 Aug 05 '19 at 15:36

0 Answers0