readdir order of files read
Started by DeathRay2K, Dec 10 2005 12:53 AM
3 replies to this topic
#1
Posted 10 December 2005 - 12:53 AM
In what order are files in a directory read using readdir()?
Also, is there any way to change it?
I want to be able to have all the folders in a directory shown before the files in the directory, but can't seem to make a folder change its position in the list...
Also, is there any way to change it?
I want to be able to have all the folders in a directory shown before the files in the directory, but can't seem to make a folder change its position in the list...
#2
Posted 10 December 2005 - 01:44 AM
The filenames are returned in the order in which they are stored by the filesystem.In what order are files in a directory read using readdir()?
Sort themAlso, is there any way to change it?
Somthing along the lines of this should work, and it sorts them as well.I want to be able to have all the folders in a directory shown before the files in the directory, but can't seem to make a folder change its position in the list...
if($dh = opendir($path)){ while (false !== ($file=readdir($dh))) { if(is_dir($file)) $dirs[] = $file . '/'; //Put directorys in dirs[] and append a / to differentiate else $files[] = $file; } } //sort files and dirs alphabetically if($dirs) natcasesort($dirs); if($files) natcasesort($files); //merge dirs and files with dirs first $files = array_merge($dirs,$files);
Star Wars Tactics- A Star Wars Gaming Site
Empire at War Zone- An Empire at War Modding Site
Battle for the Galaxy- A Star Wars Mod For The Battle for Middle-earth 2
#3
Posted 10 December 2005 - 02:14 AM
Thanks very much!
I'm going to have to remake a couple hundred lines of PHP to make my script work like this, but if it'll sort directories and then files, its worth it.
Hmm, I don't know what I'm doing wrong now...
This is the whole part of my code that lists the downloads and categories and subcategories. :|
if (is_dir($newdir)) {
if($dh = opendir($newdir)){
if(isset($subcat)){
echo '<div id="category"><a name="Back"></a><b>Back</b><br><div id="download"><a href="' . $fn . '.php?cat=' . $cat . '">Go</a></div></div><hr>';
}
elseif(isset($cat)){
echo '<div id="category"><a name="Back"></a><b>Back</b><br><div id="download"><a href="' . $fn . '.php">Go</a></div></div><hr>';
}
$i = 0;
while (($file = readdir($dh)) !== false) {
$fileinfo = explode('|', $file);
$dpf = explode('.', $file);
if(count($fileinfo)<2){
$type = 'File';
}
elseif($fileinfo[1] == 'Category'){
$type = 'Category';
}
elseif($fileinfo[1] == 'Subcategory'){
$type = 'Subcategory';
}
elseif($dpf >=3){
$type = 'Undesirable';
}
if($type == 'Category'){
$cats[$i] = $file;
if($ndh = opendir($dir . '/' . $file)){
$num = 0;
while(($read = readdir($ndh)) !== false){
$num++;
}
}
closedir($ndh);
}
elseif($type == 'Subcategory'){
$subcats[$i] = $file;
if($ndh = opendir($dir . '/' . $cat . '/' . $file)){
$num = 0;
while(($read = readdir($ndh)) !== false){
$num++;
}
}
closedir($ndh);
}
elseif($type == 'File'){
$files[$i] = $file;
$size[$i] = round(filesize($dir . '/' . $cat . '/' . $subcat . '/' . $file)/1024/1024, 2);
$ratingfile = $dir . '/log/' . $file . '-r.txt';
if(file_exists($ratingfile)){
$addrates = 0;
$rates = file($ratingfile);
$numratings[$i] = count($rates);
$tempaddrates = floatval($addrates);
for($ratearray = 0; $ratearray < $numratings; $ratearray += 1){
$temprates = $rates[$ratearray];
$tempaddrates += floatval($temprates);
}
$rating[$i] = round($tempaddrates/$numratings, 1);
$ratingmsg[$i] = 'This file is rated ' . $rating . '/5.';
}
else{
$numratings[$i] = 0;
$ratingmsg[$i] = 'This file has not yet been rated.';
}
$namefind = explode('.',$file);
if(file_exists($dir . '/info/' . $file . '-n.txt')){
$namefile = $dir . '/info/' . $file . '-n.txt'; //The location of the log file, as $log
$nameread = fopen($namefile, 'r'); //New filehandle called $logread for reading the end result of the log
$name = fread($nameread, filesize($namefile)); //This actually reads the new log as $n
fclose($nameread); //Close the $logread filehandle
}
else{
$name = $namefind['0'];
}
if(file_exists($dir . '/log/' . $file . '.txt')){
$log = $dir . '/log/' . $file . '.txt'; //The location of the log file, as $log
$logread = fopen($dir . '/log/' . $file . '.txt', 'r'); //New filehandle called $logread for reading the end result of the log
$n[] = fread($logread, filesize($log)); //This actually reads the new log as $n
fclose($logread); //Close the $logread filehandle
}
else{
$n[$i] = 0;
}
$filecount++;
$i++;
}
}
natcasesort($cats);
natcasesort($subcats);
natcasesort($files);
for($f = 0; ($f >= $minfiles) & ($f <= $maxfiles) & ($type == 'Category'); $f++){
?>
<div id="category">
<?php echo $cats[$f]; ?><br>
<div id="download">
<a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>">Download</a><br>
</div>
<?php
}
for($f = 0; ($f >= $minfiles) & ($f <= $maxfiles) & ($type == 'Subcategory'); $f++){
?>
<div id="category">
<?php echo $cats[$f]; ?><br>
<div id="download">
<a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>">Go</a><br>
</div>
<?php
}
for($f = 0; ($f >= $minfiles) & ($f <= $maxfiles) & ($type == 'File'); $f++){
?>
<div id="file">
<?php echo $cats[$f]; ?><br>
<div id="download">
<a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>&page=<?php echo $currpage; ?>&file=<?php echo $files[$f]; ?>&mode=dl">Download</a><br>
<a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>&page=<?php echo $currpage; ?>&file=<?php echo $files[$f]; ?>&mode=info#rate">Rate</a><br>
</div>
<div id="downloaded">
<?php echo $size[$f]; ?>MB<br>
Downloaded <?php echo $n[$f]; ?> times<br>
<?php echo $ratingmsg[$f]; ?><br>
<a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>&page=<?php echo $currpage; ?>&file=<?php echo $files[$f]; ?>&mode=info">File Details</a>
</div>
<?php
}
if(isset($_GET['cat'])){
if(count(explode('.',$filecount/$fpp)) ==2){
$numpages = round($filecount/$fpp);
}
else{
$numpages = $filecount/$fpp;
}
if($numpages < ($filecount/$fpp)){
$numpages++;
}
$numpages = $numpages-1;
if($maxfiles <= $filecount & $currpage==1){
echo '<hr><div id="category"><b>' . $cat . ' - Page ' . $currpage . '</b><br><div id="download">Displaying files ' . $minfiles . '-' . $maxfiles . ' of ' . $filecount . '<br></div><div id="downloaded"><a href="' . $fn . '.php?cat=' . $cat . '&page=' . $nextpage . '">Next Page</a><br>Go to page ';
while($i <= $numpages){
$i = $i+1;
echo '<a href="' . $fn . '.php?cat=' . $cat . '&page=' . $i . '">' . $i . '</a> ';
}
echo '</div></div>';
}
elseif($maxfiles <= $filecount & $currpage != $numpages+1 & $currpage != 1){
echo '<hr><div id="category"><b>' . $cat . ' - Page ' . $currpage . '</b><br><div id="download">Displaying files ' . $minfiles . '-' . $maxfiles . ' of ' . $filecount . '<br></div><div id="downloaded"><a href="' . $fn . '.php?cat=' . $cat . '&page=' . $prevpage . '">Previous Page</a> - <a href="' . $fn . '.php?cat=' . $cat . '&page=' . $nextpage . '">Next Page</a><br>Go to page ';
while($i <= $numpages){
$i = $i+1;
echo '<a href="' . $fn . '.php?cat=' . $cat . '&page=' . $i . '">' . $i . '</a> ';
}
echo '</div></div>';
}
elseif($maxfiles <= $filecount & $currpage==$numpages+1 & currpage > 1){
echo '<hr><div id="category"><b>' . $cat . ' - Page ' . $currpage . '</b><br><div id="download">Displaying files ' . $minfiles . '-' . $maxfiles . ' of ' . $filecount . '<br></div><div id="downloaded"><a href="' . $fn . '.php?cat=' . $cat . '&page=' . $prevpage . '">Previous Page</a><br>Go to page ';
while($i <= $numpages){
$i = $i+1;
echo '<a href="' . $fn . '.php?cat=' . $cat . '&page=' . $i . '">' . $i . '</a> ';
}
echo '</div></div>';
}
elseif($maxfiles >= $filecount & $currpage==1){
echo '<hr><div id="category"><b>' . $cat . ' - Page ' . $currpage . '</b><br><div id="download">Displaying files ' . $minfiles . '-' . $filecount . ' of ' . $filecount . '<br></div><div id="downloaded">Go to page ';
while($i <= $numpages){
$i = $i+1;
echo '<a href="' . $fn . '.php?cat=' . $cat . '&page=' . $i . '">' . $i . '</a> ';
}
echo '</div></div>';
}
elseif($maxfiles >= $filecount){
echo '<hr><div id="category"><b>' . $cat . ' - Page ' . $currpage . '</b><br><div id="download">Displaying files ' . $minfiles . '-' . $filecount . ' of ' . $filecount . '<br></div><div id="downloaded"><a href="' . $fn . '.php?cat=' . $cat . '&page=' . $prevpage . '">Previous Page</a><br>Go to page ';
while($i <= $numpages){
$i = $i+1;
echo '<a href="' . $fn . '.php?cat=' . $cat . '&page=' . $i . '">' . $i . '</a> ';
}
echo '</div></div>';
}
}
}
closedir($dh);
}
else {
echo 'Directory does not exist';
}
I'm going to have to remake a couple hundred lines of PHP to make my script work like this, but if it'll sort directories and then files, its worth it.
Hmm, I don't know what I'm doing wrong now...
This is the whole part of my code that lists the downloads and categories and subcategories. :|
CODE
if (is_dir($newdir)) {
if($dh = opendir($newdir)){
if(isset($subcat)){
echo '<div id="category"><a name="Back"></a><b>Back</b><br><div id="download"><a href="' . $fn . '.php?cat=' . $cat . '">Go</a></div></div><hr>';
}
elseif(isset($cat)){
echo '<div id="category"><a name="Back"></a><b>Back</b><br><div id="download"><a href="' . $fn . '.php">Go</a></div></div><hr>';
}
$i = 0;
while (($file = readdir($dh)) !== false) {
$fileinfo = explode('|', $file);
$dpf = explode('.', $file);
if(count($fileinfo)<2){
$type = 'File';
}
elseif($fileinfo[1] == 'Category'){
$type = 'Category';
}
elseif($fileinfo[1] == 'Subcategory'){
$type = 'Subcategory';
}
elseif($dpf >=3){
$type = 'Undesirable';
}
if($type == 'Category'){
$cats[$i] = $file;
if($ndh = opendir($dir . '/' . $file)){
$num = 0;
while(($read = readdir($ndh)) !== false){
$num++;
}
}
closedir($ndh);
}
elseif($type == 'Subcategory'){
$subcats[$i] = $file;
if($ndh = opendir($dir . '/' . $cat . '/' . $file)){
$num = 0;
while(($read = readdir($ndh)) !== false){
$num++;
}
}
closedir($ndh);
}
elseif($type == 'File'){
$files[$i] = $file;
$size[$i] = round(filesize($dir . '/' . $cat . '/' . $subcat . '/' . $file)/1024/1024, 2);
$ratingfile = $dir . '/log/' . $file . '-r.txt';
if(file_exists($ratingfile)){
$addrates = 0;
$rates = file($ratingfile);
$numratings[$i] = count($rates);
$tempaddrates = floatval($addrates);
for($ratearray = 0; $ratearray < $numratings; $ratearray += 1){
$temprates = $rates[$ratearray];
$tempaddrates += floatval($temprates);
}
$rating[$i] = round($tempaddrates/$numratings, 1);
$ratingmsg[$i] = 'This file is rated ' . $rating . '/5.';
}
else{
$numratings[$i] = 0;
$ratingmsg[$i] = 'This file has not yet been rated.';
}
$namefind = explode('.',$file);
if(file_exists($dir . '/info/' . $file . '-n.txt')){
$namefile = $dir . '/info/' . $file . '-n.txt'; //The location of the log file, as $log
$nameread = fopen($namefile, 'r'); //New filehandle called $logread for reading the end result of the log
$name = fread($nameread, filesize($namefile)); //This actually reads the new log as $n
fclose($nameread); //Close the $logread filehandle
}
else{
$name = $namefind['0'];
}
if(file_exists($dir . '/log/' . $file . '.txt')){
$log = $dir . '/log/' . $file . '.txt'; //The location of the log file, as $log
$logread = fopen($dir . '/log/' . $file . '.txt', 'r'); //New filehandle called $logread for reading the end result of the log
$n[] = fread($logread, filesize($log)); //This actually reads the new log as $n
fclose($logread); //Close the $logread filehandle
}
else{
$n[$i] = 0;
}
$filecount++;
$i++;
}
}
natcasesort($cats);
natcasesort($subcats);
natcasesort($files);
for($f = 0; ($f >= $minfiles) & ($f <= $maxfiles) & ($type == 'Category'); $f++){
?>
<div id="category">
<?php echo $cats[$f]; ?><br>
<div id="download">
<a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>">Download</a><br>
</div>
<?php
}
for($f = 0; ($f >= $minfiles) & ($f <= $maxfiles) & ($type == 'Subcategory'); $f++){
?>
<div id="category">
<?php echo $cats[$f]; ?><br>
<div id="download">
<a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>">Go</a><br>
</div>
<?php
}
for($f = 0; ($f >= $minfiles) & ($f <= $maxfiles) & ($type == 'File'); $f++){
?>
<div id="file">
<?php echo $cats[$f]; ?><br>
<div id="download">
<a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>&page=<?php echo $currpage; ?>&file=<?php echo $files[$f]; ?>&mode=dl">Download</a><br>
<a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>&page=<?php echo $currpage; ?>&file=<?php echo $files[$f]; ?>&mode=info#rate">Rate</a><br>
</div>
<div id="downloaded">
<?php echo $size[$f]; ?>MB<br>
Downloaded <?php echo $n[$f]; ?> times<br>
<?php echo $ratingmsg[$f]; ?><br>
<a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>&page=<?php echo $currpage; ?>&file=<?php echo $files[$f]; ?>&mode=info">File Details</a>
</div>
<?php
}
if(isset($_GET['cat'])){
if(count(explode('.',$filecount/$fpp)) ==2){
$numpages = round($filecount/$fpp);
}
else{
$numpages = $filecount/$fpp;
}
if($numpages < ($filecount/$fpp)){
$numpages++;
}
$numpages = $numpages-1;
if($maxfiles <= $filecount & $currpage==1){
echo '<hr><div id="category"><b>' . $cat . ' - Page ' . $currpage . '</b><br><div id="download">Displaying files ' . $minfiles . '-' . $maxfiles . ' of ' . $filecount . '<br></div><div id="downloaded"><a href="' . $fn . '.php?cat=' . $cat . '&page=' . $nextpage . '">Next Page</a><br>Go to page ';
while($i <= $numpages){
$i = $i+1;
echo '<a href="' . $fn . '.php?cat=' . $cat . '&page=' . $i . '">' . $i . '</a> ';
}
echo '</div></div>';
}
elseif($maxfiles <= $filecount & $currpage != $numpages+1 & $currpage != 1){
echo '<hr><div id="category"><b>' . $cat . ' - Page ' . $currpage . '</b><br><div id="download">Displaying files ' . $minfiles . '-' . $maxfiles . ' of ' . $filecount . '<br></div><div id="downloaded"><a href="' . $fn . '.php?cat=' . $cat . '&page=' . $prevpage . '">Previous Page</a> - <a href="' . $fn . '.php?cat=' . $cat . '&page=' . $nextpage . '">Next Page</a><br>Go to page ';
while($i <= $numpages){
$i = $i+1;
echo '<a href="' . $fn . '.php?cat=' . $cat . '&page=' . $i . '">' . $i . '</a> ';
}
echo '</div></div>';
}
elseif($maxfiles <= $filecount & $currpage==$numpages+1 & currpage > 1){
echo '<hr><div id="category"><b>' . $cat . ' - Page ' . $currpage . '</b><br><div id="download">Displaying files ' . $minfiles . '-' . $maxfiles . ' of ' . $filecount . '<br></div><div id="downloaded"><a href="' . $fn . '.php?cat=' . $cat . '&page=' . $prevpage . '">Previous Page</a><br>Go to page ';
while($i <= $numpages){
$i = $i+1;
echo '<a href="' . $fn . '.php?cat=' . $cat . '&page=' . $i . '">' . $i . '</a> ';
}
echo '</div></div>';
}
elseif($maxfiles >= $filecount & $currpage==1){
echo '<hr><div id="category"><b>' . $cat . ' - Page ' . $currpage . '</b><br><div id="download">Displaying files ' . $minfiles . '-' . $filecount . ' of ' . $filecount . '<br></div><div id="downloaded">Go to page ';
while($i <= $numpages){
$i = $i+1;
echo '<a href="' . $fn . '.php?cat=' . $cat . '&page=' . $i . '">' . $i . '</a> ';
}
echo '</div></div>';
}
elseif($maxfiles >= $filecount){
echo '<hr><div id="category"><b>' . $cat . ' - Page ' . $currpage . '</b><br><div id="download">Displaying files ' . $minfiles . '-' . $filecount . ' of ' . $filecount . '<br></div><div id="downloaded"><a href="' . $fn . '.php?cat=' . $cat . '&page=' . $prevpage . '">Previous Page</a><br>Go to page ';
while($i <= $numpages){
$i = $i+1;
echo '<a href="' . $fn . '.php?cat=' . $cat . '&page=' . $i . '">' . $i . '</a> ';
}
echo '</div></div>';
}
}
}
closedir($dh);
}
else {
echo 'Directory does not exist';
}
My problem is that nothing is displaying at all...
Edited by DeathRay2K, 10 December 2005 - 03:50 AM.
#4
Posted 10 December 2005 - 07:41 PM
Sorry for the double post, but I thought I'd figured it out.
Now my problem is that only the first thing alphabetically in the directory is displayed.
I have it split into 3 arrays, one for categories, one for subcategories, and one for files.
Here is the code for displaying them:
Edit:
Stucuk advised me that those won't work, and sugested this:
But it still didn't have any effect... :(
Now my problem is that only the first thing alphabetically in the directory is displayed.
I have it split into 3 arrays, one for categories, one for subcategories, and one for files.
Here is the code for displaying them:
for($f = 0; $type[$f] == 'Category'; $f++){ ?> <div id="category"> <?php echo $cats[$f]; ?><br> <div id="download"> <a href="<?php echo $fn; ?>.php?cat=<?php echo $cats[$f]; ?>">Go</a><br> </div> </div> <?php } for($f = 0; $type[$f] == 'Subcategory'; $f++){ ?> <div id="category"> <?php echo $subcats[$f]; ?><br> <div id="download"> <a href="<?php echo $fn; ?>.php?cat=<?php echo $catname; ?>&subcat=<?php echo $subcats[$f]; ?>">Go</a><br> </div> </div> <?php } for($f = 0; $type[$f] == 'File'; $f++){ ?> <div id="file"> <?php echo $files[$f]; ?><br> <div id="download"> <a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>&page=<?php echo $currpage; ?>&file=<?php echo $files[$f]; ?>&mode=dl">Download</a><br> <a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>&page=<?php echo $currpage; ?>&file=<?php echo $files[$f]; ?>&mode=info#rate">Rate</a><br> </div> <div id="downloaded"> <?php echo $size[$f]; ?>MB<br> Downloaded <?php echo $n[$f]; ?> times<br> <?php echo $ratingmsg[$f]; ?><br> <a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>&page=<?php echo $currpage; ?>&file=<?php echo $files[$f]; ?>&mode=info">File Details</a> </div> </div> <?php }
Edit:
Stucuk advised me that those won't work, and sugested this:
for($f = 0; $f<$numstuff; $f++){ echo $type[$f] . '<br>'; if($type[$f] == 'Category'){ ?> <div id="category"> <a name="<?php echo $cats[$f]; ?>"></a> <?php echo $cats[$f]; ?><br> <div id="download"> <a href="<?php echo $fn; ?>.php?cat=<?php echo $cats[$f]; ?>">Go</a><br> </div> </div> <?php } } for($f = 0; $f<$numstuff; $f++){ echo $type[$f] . '<br>'; if($type[$f] == 'Subcategory'){ ?> <div id="category"> <a name="<?php echo $subcats[$f]; ?>"></a> <?php echo $subcats[$f]; ?><br> <div id="download"> <a href="<?php echo $fn; ?>.php?cat=<?php echo $catname; ?>&subcat=<?php echo $subcats[$f]; ?>">Go</a><br> </div> </div> <?php } } for($f = 0; $f<$numstuff; $f++){ echo $type[$f] . '<br>'; if($type[$f] == 'File'){ ?> <div id="file"> <a name="<?php echo $files[$f]; ?>"></a> <?php echo $files[$f]; ?><br> <div id="download"> <a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>&page=<?php echo $currpage; ?>&file=<?php echo $files[$f]; ?>&mode=dl">Download</a><br> <a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>&page=<?php echo $currpage; ?>&file=<?php echo $files[$f]; ?>&mode=info#rate">Rate</a><br> </div> <div id="downloaded"> <?php echo $size[$f]; ?>MB<br> Downloaded <?php echo $n[$f]; ?> times<br> <?php echo $ratingmsg[$f]; ?><br> <a href="<?php echo $fn; ?>.php?cat=<?php echo $cat; ?>&subcat=<?php echo $subcat; ?>&page=<?php echo $currpage; ?>&file=<?php echo $files[$f]; ?>&mode=info">File Details</a> </div> </div> <?php } }
But it still didn't have any effect... :(
Edited by DeathRay2K, 11 December 2005 - 01:22 AM.