phppup Posted December 12, 2019 Author Share Posted December 12, 2019 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/309639-sort-directory-of-folder/page/2/#findComment-1572476 Share on other sites More sharing options...
Psycho Posted December 12, 2019 Share Posted December 12, 2019 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. Quote 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); if($globFiles===false) { echo "glob('{$pattern}') returned an error"; } else { echo "Results of glob('{$pattern}'): <pre>" . print_r($globFiles, 1) . "</pre>"; } Quote Link to comment https://forums.phpfreaks.com/topic/309639-sort-directory-of-folder/page/2/#findComment-1572478 Share on other sites More sharing options...
phppup Posted December 13, 2019 Author Share Posted December 13, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309639-sort-directory-of-folder/page/2/#findComment-1572480 Share on other sites More sharing options...
phppup Posted December 13, 2019 Author Share Posted December 13, 2019 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; to 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. Quote Link to comment https://forums.phpfreaks.com/topic/309639-sort-directory-of-folder/page/2/#findComment-1572481 Share on other sites More sharing options...
phppup Posted December 13, 2019 Author Share Posted December 13, 2019 How can I reverse sort from Z to A? Or by last modified? Quote Link to comment https://forums.phpfreaks.com/topic/309639-sort-directory-of-folder/page/2/#findComment-1572482 Share on other sites More sharing options...
Psycho Posted December 13, 2019 Share Posted December 13, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309639-sort-directory-of-folder/page/2/#findComment-1572483 Share on other sites More sharing options...
Barand Posted December 13, 2019 Share Posted December 13, 2019 8 hours ago, phppup said: //not sure if it better seperated since listing file NAMES would be neater than the ENTIRE PATH Then use basename() Quote Link to comment https://forums.phpfreaks.com/topic/309639-sort-directory-of-folder/page/2/#findComment-1572487 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.