Jump to content

SORT directory of folder


Recommended Posts

I've been at it all day, but half (ok, maybe just a third) of what I'm reading may as well be in Chinese.

And another third is incorrect samples and misinformation that doesn't even work.

That leaves my own ingenuity, and help form you.

I tried using  the example you provided, as well as visiting the PHP.net page, and was not able to get results.  Doesn't the glob() need a reference directory?  It's not simply going to automatically search the entire server, is it?

At least now I understand why slapping a glob into glace in conjunction with a WHILE seems to blow-up the script and scramble the results to something that destroys the gallery display (most of the time).

I understand a good portion of what you told me, but do appreciate the greater enlightenment.

At this point I don't even want the answer given to me, but I'm still having trouble with getting a simple result from 

$globFiles = glob('*');
echo "<pre>" . print_r($globFiles, 1) . "</pre>";

//OR the PHP.net sample

foreach (glob("*.txt") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";


Link to comment
Share on other sites

You're saying this produced no results?

$globFiles = glob('*');
echo "<pre>" . print_r($globFiles, 1) . "</pre>";

I find that hard to believe. If you were only trying the code from the manual then, yes, I could see that not producing any results because you many not have ant "txt" files in the directory being scanned.


Doesn't the glob() need a reference directory? 

Yes it does. And '*' is a reference because the path can be relative from the current working directory. Using '*' should return all the files in the current working directory (i.e. the directory in which the script is executed from). That is why I find it hard to believe that the test code I asked you to try earlier is producing no results. Just to be sure I did not make a mistake, I just ran that code again and it returned all the contents of the directory where I ran the script as expected. Is it returning an empty array or an error? Try this test script and paste the results here:

$pattern = '*';

$globFiles = glob($pattern);
    echo "glob('{$pattern}') returned an error";
    echo "Results of glob('{$pattern}'): <pre>" . print_r($globFiles, 1) . "</pre>";


Link to comment
Share on other sites

I was thrilled to have gotten a recognizable result after tinkering with the script form the manual and using

foreach (glob("upload/*.jpg") as $filename) {
    echo "$filename size " . filesize($filename) . "<br />";

I have now run both your scripts and gotten the same output which was an array designated listing of the files in the root directory (including the script itself).

Now I am still wrestling with replacing the WHILE with a FOREACH.

I have found alternate scripts that seem more simplified than this one.

Maybe the recursive style is causing me trouble [not even sure if the approach is really favorable] but I would like to meet this challenge successfully.

Link to comment
Share on other sites

This has got to be the UGLIEST CODE that you've ever seen, but I'm sure you'll take some conciliatory victory laps in knowing that IT WORKS.

I went from the original 

echo scanDirectoryImages("uploaded");

    $directoryList = opendir($directory);
        while($file = readdir($directoryList)) {
            if ($file != '.' && $file != '..') {
                $path = $directory . '/' . $file;


echo scanDirectoryImages("uploads");

      //  $directoryList = opendir($directory);     // no longer necessary to create the handle 
$directory = "uploads";                                    //establishes the folder to be located and scanned
      //  while($file = readdir($directoryList)) {  // REMOVED as the wrong approach for glob() 

       foreach (glob("$directory/*") as $file) {  
//OR   foreach (glob($directory."/*") as $file) {  
                                          //BOTH WORK with either QUOTE MARK scheme (is one more correct?)

//foreach (glob("$d/*") as $file) {  //part of the Learning Process to determine if a variable needed different handling than a hardcoded routing

/***** SAMPLE to work with and TEST *****/
//foreach (glob("uploads/*") as $filename) {
    //echo "$filename size " . filesize($filename) . "<br />";

echo "$file size " . filesize($file) . "<br />";  //PROVED SUCCESS using variable name $file to avoid conflicting names from TESTing to established script

            if ($file != '.' && $file != '..') {       //no longer necessary, but will not interfere. Let's get functional before pretty

// $path = $directory . '/' . $file;
   $path =  $file;                       //the NEW variable $file INCLUDES $directory AND slash; this was easiest solution.
                                         //And it worked.
                                         //not sure if it better seperated since listing file NAMES would be neater than the ENTIRE PATH
                                         //Best way to break it up????

I hope this validates the sincerity that you doubted earlier.

Perhaps you can offer a few (or bunches) of tips for better functionality and clean up.

PS: I'm still considering a different/more simplistic script and wonder in recursion is beneficial or just stylish.

Thanks for the guidance.

Link to comment
Share on other sites

2 hours ago, phppup said:

How can I reverse sort from Z to A?

Or by last modified?

First, use glob() to store the results into an array variable. Then you can perform operations on that array BEFORE you use the foreach() loop to generate the output. E.g.

//Get the files in the directory
$filesAry = glob("{$directory}/*");
//Sort the array in reverse order
rsort($filesAry );

//Output the results
foreach($filesAry as $file)
    //Create the output

If you want to sort by date/size/etc, then where I have rsort() above, I would run a foreach() loop over the file names and create a multi-dimensional array with all the data, then you can use usort() with a custom function to sort however you wish.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.