Read directory, sort and echo file names

Hello everyone,

Dave here. I’m new to the forum and I’m a “now and then” programmer of simple PHP. I’m on a project at work where I am the developer. (Fun stuff - no access to a database or FTP, all file uploads via a Movable Type interface, but I’m not building a Movable Type site.)

Anyway, my problem is this. I need to read the contents of a directory and echo the file names in numerical order (actually, in the long run, I need to echo the contents of the file, sorted numerically by file name, but that’s a hurdle I’ll cross once I get this figured out).

So far, I can read and echo the file names, but I can’t seem to sort them. I’ve tried various array things, but I’m starting to get in over my head. They keep echoing in the same order, so I’m assuming my attempts to sort and work with arrays isn’t working.

I’ve got a simple test script I’m using to break and fix as I try to figure it out. Here’s what I’ve got:

[php]

<?php if ($handle1 = opendir('./event-titles')) { while (false !== ($entry1 = readdir($handle1))) { if ($entry1 != "." && $entry1 != "..") { $remoteFile = "event-titles/".$entry1; $remoteFileContents = file_get_contents($remoteFile); echo "".$remoteFile."

"; } } closedir($handle1); } ?>

[/php]

I’m surprised at how far I’ve gotten with other PHP-related parts of the site, but if you saw the code I’ve written you’d reach for the Maalox. Anything I do that needs to be stored gets written to a text file since databases aren’t available for me. What a joy. Anyway, ANY help anybody can provide would be appreciated. I really try to figure these things out on my own, and this is the first quandry that’s had me stumped to the point of begging for help. :wink:

Dave

(More fun stuff - I’m stuck with Windows XP and Internet Explorer 8 here at work.)

I believe this is what you are looking for: http://www.php.net/manual/en/function.sort.php

it takes in the array and sorts the contents, without maintaining the index

I think I’m getting closer albertle5, although now I’m echoing “remoteFile[] =” once for each file in the directory. Obviously I’m doing something wrong, and I’m sure that 90% of it is a lack of understanding on my part.

Also, thanks to plintu for pointing me to a PHP file manager script that lets me bypass all the Movable Type hassle.

So, can anyone give me an idea of what I’m doing wrong with the code below? (I’m studying up on PHP outside of work, but this assignment kind of caught me off guard, so I’m working in areas I haven’t studied yet. Ouch!)

Dave

[php]

<?php if ($handle1 = opendir('./event-titles')) { while (false !== ($entry1 = readdir($handle1))) { if ($entry1 != "." && $entry1 != "..") { $remoteFile = "event-titles/".$entry1; sort($remoteFile); $remoteFileContents = file_get_contents($remoteFile); foreach ($remoteFile as $key => $val); { echo "remoteFile[" . $key . "] = " . $val . "\n"; } } } closedir($handle1); } ?>

[/php]

Here’s an update about what I’m trying to do. I’ve got sorting sorted out (pun intended), but I need to echo the contents of the files, with the contents sorted by their file names (I’m creating a list of events that need to be listed in chronological order). Eventually, once I’ve got this working, I’ll stop echoing the file name - I’ve just got them here for testing. Hopefully you can follow the logic concentrated in line 14. Right now what I’m getting is an echoed file number, followed by line breaks where the file content should be, and then the next file name. The file names are successfully being sorted (finally).

[php]

<?php $dirFiles = array(); if ($handle = opendir('./event-titles')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $dirFiles[] = $file; } } closedir($handle); } sort($dirFiles); foreach($dirFiles as $file) { $fileContents = file_get_contents($file); echo $file."
".$fileContents."

"; } ?>

[/php]

it seems that you are getting confused on how arrays in php store elements try inserting
[php]$print_r($dirFiles);[/php] after your sort statement

To output an array element you want to [php]echo $dirFiles[‘0’];[/php], where the ‘0’ is the index of the element in the array

your problem is your are getting file contents if the full array rather than an element of the array

good luck

ps - the new way of writing
is
which is better

Yep, it goes without saying that I’m confused about arrays. Before just recently, I thought array was a drop of golden sun. ;D (See Wikipedia if you don’t know what do re mi is - can’t link from here.)

I’m apparently still not getting the full concept though. If I try the array output as I thought it might be included…

[php]echo $file."
".$fileContents[‘0’]."

[/php]…

it still doesn’t work for me - I still get the file names echoed but no associated content following each file name.

I hope I’m not bugging anyone with this. I keep digging through Google trying to find an "ah ha’ moment that I’ll catch on to, but I have a feeling I’m not doing very will with my searches. Thankfully so far I’ve had a lot of “ah ha” moments, and now that I’ve got other code working, I have it to refer back to as I know what it’s all doing. So I AM learning, just stupidly slow. :smiley:

I will just help you fix it now since it seems like it is frustrating you more than helping you work through the problem by troubleshooting.

[php]

<?php if ($handle = opendir('./event-titles')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $dirFiles[] = $file; } } closedir($handle); } sort($dirFiles); for($i = 0, $i < sizeof($dirFiles); $i++) { $fileContents = file_get_contents($dirFiles[$i]); echo $dirFiles[$i] ."
".$fileContents."

"; } ?>

[/php]

I modified the foreach loop to a for loop (just my preference, but both work fine). Using a foreach loop you still have to use a outside variable to run through the array. I find the foreach loop sometimes makes things more confusing. Also, I removed the first statement setting the $dirfiles array, it is unneeded in php. When you are assigning values to the array as long as you have the [] after the variable you want the values to go into it will be set in it.

Sponsor our Newsletter | Privacy Policy | Terms of Service