ea7p0 Posted March 16, 2008 Share Posted March 16, 2008 Hello everybody based on a script from http://www.evoluted.net/community/code/directorylisting.ph I want to included a list of files in a folder. And basically everything is working fine, except for one problem: I cannot change the sorting-criteria according to which the files are displayed. As I understand the script posted below, the primary criterion is "date" and then "size" but not "name". However the files appear in alphabetical order and I cannot change it. Does anybody know what the problem might be? Cheers. <? $startdir = '../wettin/cms/uploader/uploads/docs'; $showthumbnails = true; $showdirs = false; $forcedownloads = false; $hide = array( 'dlf', 'index.php', 'Thumbs', '.htaccess', '.htpasswd' ); $displayindex = false; $allowuploads = false; $overwrite = false; $indexfiles = array ( 'index.html', 'index.htm', 'default.htm', 'default.html' ); $filetypes = array ( 'png' => 'jpg.gif', 'jpeg' => 'jpg.gif', 'bmp' => 'jpg.gif', 'jpg' => 'jpg.gif', 'gif' => 'gif.gif', 'zip' => 'archive.png', 'rar' => 'archive.png', 'exe' => 'exe.gif', 'setup' => 'setup.gif', 'txt' => 'text.png', 'htm' => 'html.gif', 'html' => 'html.gif', 'fla' => 'fla.gif', 'swf' => 'swf.gif', 'xls' => 'xls.gif', 'doc' => 'doc.gif', 'sig' => 'sig.gif', 'fh10' => 'fh10.gif', 'pdf' => 'pdf.gif', 'psd' => 'psd.gif', 'rm' => 'real.gif', 'mpg' => 'video.gif', 'mpeg' => 'video.gif', 'mov' => 'video2.gif', 'avi' => 'video.gif', 'eps' => 'eps.gif', 'gz' => 'archive.png', 'asc' => 'sig.gif', ); error_reporting(0); if(!function_exists('imagecreatetruecolor')) $showthumbnails = false; $leadon = $startdir; if($leadon=='.') $leadon = ''; if((substr($leadon, -1, 1)!='/') && $leadon!='') $leadon = $leadon . '/'; $startdir = $leadon; if($_GET['dir']) { if(substr($_GET['dir'], -1, 1)!='/') { $_GET['dir'] = $_GET['dir'] . '/'; } $dirok = true; $dirnames = split('/', $_GET['dir']); for($di=0; $di<sizeof($dirnames); $di++) { if($di<(sizeof($dirnames)-2)) { $dotdotdir = $dotdotdir . $dirnames[$di] . '/'; } if($dirnames[$di] == '..') { $dirok = false; } } if(substr($_GET['dir'], 0, 1)=='/') { $dirok = false; } if($dirok) { $leadon = $leadon . $_GET['dir']; } } if($_GET['download'] && $forcedownloads) { $file = str_replace('/', '', $_GET['download']); $file = str_replace('..', '', $file); if(file_exists($leadon . $file)) { header("Content-type: application/x-download"); header("Content-Length: ".filesize($leadon . $file)); header('Content-Disposition: attachment; filename="'.$file.'"'); readfile($leadon . $file); die(); } } $opendir = $leadon; if(!$leadon) $opendir = '.'; if(!file_exists($opendir)) { $opendir = '.'; $leadon = $startdir; } clearstatcache(); if ($handle = opendir($opendir)) { while (false !== ($file = readdir($handle))) { if ($file == "." || $file == "..") continue; $discard = false; for($hi=0;$hi<sizeof($hide);$hi++) { if(strpos($file, $hide[$hi])!==false) { $discard = true; } } if($discard) continue; if (@filetype($leadon.$file) == "dir") { if(!$showdirs) continue; $n++; if($_GET['sort']=="date") { $key = @filemtime($leadon.$file) . ".$n"; } else { $key = $n; } $dirs[$key] = $file . "/"; } else { $n++; if($_GET['sort']=="date") { $key = @filemtime($leadon.$file) . ".$n"; } elseif($_GET['sort']=="size") { $key = @filesize($leadon.$file) . ".$n"; } else { $key = $n; } $files[$key] = $file; if($displayindex) { if(in_array(strtolower($file), $indexfiles)) { header("Location: $file"); die(); } } } } closedir($handle); } if($_GET['sort']=="date") { @ksort($dirs, SORT_NUMERIC); @ksort($files, SORT_NUMERIC); } elseif($_GET['sort']=="size") { @natcasesort($dirs); @ksort($files, SORT_NUMERIC); } else { @natcasesort($date); @natcasesort($dirs); } if($_GET['order']=="desc" && $_GET['sort']!="date") {$dirs = @array_reverse($dirs);} if($_GET['order']=="desc") {$files = @array_reverse($files);} $dirs = @array_values($dirs); $files = @array_values($files); ?> Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted March 16, 2008 Share Posted March 16, 2008 i don't even see where the query is. Quote Link to comment Share on other sites More sharing options...
ea7p0 Posted March 16, 2008 Author Share Posted March 16, 2008 shouldn't the sorting criteria be laid out in the function? Anyways. the query looks like this: <? $class = 'b'; if($dirok) { ?> </p> <div><a href="<?=$_SERVER['../PHP_SELF'].'?dir='.urlencode($dotdotdir);?>" class="<?=$class;?>"><?=date ("M d Y h:i:s A", filemtime($dotdotdir));?></a></div> <? if($class=='b') $class='w'; else $class = 'b'; } $arsize = sizeof($dirs); for($i=0;$i<$arsize;$i++) { ?> <div><a href="<?=$_SERVER['../PHP_SELF'].'?dir='.urlencode($leadon.$dirs[$i]);?>" class="<?=$class;?>"><img src="dlf/folder.png" alt="<?=$dirs[$i];?>" /> <strong><?=$dirs[$i];?></strong> <em>-</em> <?=date ("M d Y h:i:s A", filemtime($leadon.$dirs[$i]));?></a></div> <? if($class=='b') $class='w'; else $class = 'b'; } $arsize = sizeof($files); for($i=0;$i<$arsize;$i++) { $icon = 'unknown.png'; $ext = strtolower(substr($files[$i], strrpos($files[$i], '.')+1)); $supportedimages = array('gif', 'png', 'jpeg', 'jpg'); $thumb = ''; if($showthumbnails && in_array($ext, $supportedimages)) { $thumb = '<span><img src="dlf/trans.gif" alt="'.$files[$i].'" name="thumb'.$i.'" /></span>'; $thumb2 = ' onmouseover="o('.$i.', \''.urlencode($leadon . $files[$i]).'\');" onmouseout="f('.$i.');"'; } if($filetypes[$ext]) { $icon = $filetypes[$ext]; } $filename = $files[$i]; if(strlen($filename)>43) { $filename = substr($files[$i], 0, 40) . '...'; } $fileurl = $leadon . $files[$i]; if($forcedownloads) { $fileurl = $_SESSION['PHP_SELF'] . '?dir=' . urlencode($leadon) . '&download=' . urlencode($files[$i]); } ?> <div><a href="<?=$fileurl;?>" class="<?=$class;?>"<?=$thumb2;?>><img src="dlf/<?=$icon;?>" alt="<?=$files[$i];?>" /><strong> <?=$filename;?></strong> <em><?=round(filesize($leadon.$files[$i])/1024);?>KB</em> <?=date ("M d Y h:i:s A", filemtime($leadon.$files[$i]));?><?=$thumb;?></a></div> <p> <? if($class=='b') $class='w'; else $class = 'b'; } ?> Quote Link to comment Share on other sites More sharing options...
ea7p0 Posted March 16, 2008 Author Share Posted March 16, 2008 Nobody has a clue what the problem is? Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted March 16, 2008 Share Posted March 16, 2008 dude thats not a query. a query is like a mysql_query("SELECT blah"); how are you getting you info? Quote Link to comment Share on other sites More sharing options...
ea7p0 Posted March 16, 2008 Author Share Posted March 16, 2008 The information is taken directly from the folder: $startdir = '../wettin/cms/uploader/uploads/docs'; The function doesn't have a classical query. I really don't get the whole thing. That's why I can't come up with an idea how to change the order. However, it really works, but don't ask me why and how. Quote Link to comment Share on other sites More sharing options...
ea7p0 Posted March 17, 2008 Author Share Posted March 17, 2008 shouldn't the sorting criteria be laid out in the function? Anyway, the query looks like this: <? $class = 'b'; if($dirok) { ?> </p> <div><a href="<?=$_SERVER['../PHP_SELF'].'?dir='.urlencode($dotdotdir);?>" class="<?=$class;?>"><?=date ("M d Y h:i:s A", filemtime($dotdotdir));?></a></div> <? if($class=='b') $class='w'; else $class = 'b'; } $arsize = sizeof($dirs); for($i=0;$i<$arsize;$i++) { ?> <div><a href="<?=$_SERVER['../PHP_SELF'].'?dir='.urlencode($leadon.$dirs[$i]);?>" class="<?=$class;?>"><img src="dlf/folder.png" alt="<?=$dirs[$i];?>" /> <strong><?=$dirs[$i];?></strong> <em>-</em> <?=date ("M d Y h:i:s A", filemtime($leadon.$dirs[$i]));?></a></div> <? if($class=='b') $class='w'; else $class = 'b'; } $arsize = sizeof($files); for($i=0;$i<$arsize;$i++) { $icon = 'unknown.png'; $ext = strtolower(substr($files[$i], strrpos($files[$i], '.')+1)); $supportedimages = array('gif', 'png', 'jpeg', 'jpg'); $thumb = ''; if($showthumbnails && in_array($ext, $supportedimages)) { $thumb = '<span><img src="dlf/trans.gif" alt="'.$files[$i].'" name="thumb'.$i.'" /></span>'; $thumb2 = ' onmouseover="o('.$i.', \''.urlencode($leadon . $files[$i]).'\');" onmouseout="f('.$i.');"'; } if($filetypes[$ext]) { $icon = $filetypes[$ext]; } $filename = $files[$i]; if(strlen($filename)>43) { $filename = substr($files[$i], 0, 40) . '...'; } $fileurl = $leadon . $files[$i]; if($forcedownloads) { $fileurl = $_SESSION['PHP_SELF'] . '?dir=' . urlencode($leadon) . '&download=' . urlencode($files[$i]); } ?> <div><a href="<?=$fileurl;?>" class="<?=$class;?>"<?=$thumb2;?>><img src="dlf/<?=$icon;?>" alt="<?=$files[$i];?>" /><strong> <?=$filename;?></strong> <em><?=round(filesize($leadon.$files[$i])/1024);?>KB</em> <?=date ("M d Y h:i:s A", filemtime($leadon.$files[$i]));?><?=$thumb;?></a></div> <p> <? if($class=='b') $class='w'; else $class = 'b'; } ?> No matter what you call it, query or whatever, this is the part by which the folder's content is displayed. I don't think I can change very much here. Therefore I have already tried the following: <? $startdir = '../wettin/cms/uploader/uploads/docs'; $showthumbnails = true; $showdirs = false; $forcedownloads = false; $hide = array( 'dlf', 'index.php', 'Thumbs', '.htaccess', '.htpasswd' ); $displayindex = false; $allowuploads = false; $overwrite = false; $indexfiles = array ( 'index.html', 'index.htm', 'default.htm', 'default.html' ); $filetypes = array ( 'png' => 'jpg.gif', 'jpeg' => 'jpg.gif', 'bmp' => 'jpg.gif', 'jpg' => 'jpg.gif', 'gif' => 'gif.gif', 'zip' => 'archive.png', 'rar' => 'archive.png', 'exe' => 'exe.gif', 'setup' => 'setup.gif', 'txt' => 'text.png', 'htm' => 'html.gif', 'html' => 'html.gif', 'fla' => 'fla.gif', 'swf' => 'swf.gif', 'xls' => 'xls.gif', 'doc' => 'doc.gif', 'sig' => 'sig.gif', 'fh10' => 'fh10.gif', 'pdf' => 'pdf.gif', 'psd' => 'psd.gif', 'rm' => 'real.gif', 'mpg' => 'video.gif', 'mpeg' => 'video.gif', 'mov' => 'video2.gif', 'avi' => 'video.gif', 'eps' => 'eps.gif', 'gz' => 'archive.png', 'asc' => 'sig.gif', ); error_reporting(0); if(!function_exists('imagecreatetruecolor')) $showthumbnails = false; $leadon = $startdir; if($leadon=='.') $leadon = ''; if((substr($leadon, -1, 1)!='/') && $leadon!='') $leadon = $leadon . '/'; $startdir = $leadon; if($_GET['dir']) { if(substr($_GET['dir'], -1, 1)!='/') { $_GET['dir'] = $_GET['dir'] . '/'; } $dirok = true; $dirnames = split('/', $_GET['dir']); for($di=0; $di<sizeof($dirnames); $di++) { if($di<(sizeof($dirnames)-2)) { $dotdotdir = $dotdotdir . $dirnames[$di] . '/'; } if($dirnames[$di] == '..') { $dirok = false; } } if(substr($_GET['dir'], 0, 1)=='/') { $dirok = false; } if($dirok) { $leadon = $leadon . $_GET['dir']; } } if($_GET['download'] && $forcedownloads) { $file = str_replace('/', '', $_GET['download']); $file = str_replace('..', '', $file); if(file_exists($leadon . $file)) { header("Content-type: application/x-download"); header("Content-Length: ".filesize($leadon . $file)); header('Content-Disposition: attachment; filename="'.$file.'"'); readfile($leadon . $file); die(); } } $opendir = $leadon; if(!$leadon) $opendir = '.'; if(!file_exists($opendir)) { $opendir = '.'; $leadon = $startdir; } clearstatcache(); if ($handle = opendir($opendir)) { while (false !== ($file = readdir($handle))) { if ($file == "." || $file == "..") continue; $discard = false; for($hi=0;$hi<sizeof($hide);$hi++) { if(strpos($file, $hide[$hi])!==false) { $discard = true; } } if($discard) continue; if (@filetype($leadon.$file) == "dir") { if(!$showdirs) continue; $n++; if($_GET['sort']=="date") { $key = @filemtime($leadon.$file) . ".$n";//[b]I thought this might lead to sorting by date but obviously it doesn't[/b] } else { $key = $n; } $dirs[$key] = $file . "/"; } else { $n++; if($_GET['sort']=="date") { $key = @filemtime($leadon.$file) . ".$n"; } elseif($_GET['sort']=="size") { $key = @filesize($leadon.$file) . ".$n";//[b]I tried to delete this part of the function just in case it overrules the "date" part for some reason, but it didn't have any effect whatsoever[/b] } else { $key = $n; } $files[$key] = $file; if($displayindex) { if(in_array(strtolower($file), $indexfiles)) { header("Location: $file"); die(); } } } } closedir($handle); } //[b]as far as I can see the part above is only relevant for displaying the path which I have taken out anyway[/b] if($_GET['sort']=="date") { @ksort($dirs, SORT_NUMERIC); @ksort($files, SORT_NUMERIC);//this might actually be something I would have to change, but I don't no how. } elseif($_GET['sort']=="size") { @natcasesort($dirs); @ksort($files, SORT_NUMERIC); } else { @natcasesort($date); @natcasesort($dirs); } if($_GET['order']=="desc" && $_GET['sort']!="date") {$dirs = @array_reverse($dirs);} if($_GET['order']=="desc") {$files = @array_reverse($files);} $dirs = @array_values($dirs); $files = @array_values($files); ?> The funny thing really is that I can't see any sorting behaviour by 'name'. if($_GET['sort']=="date") { @ksort($dirs, SORT_NUMERIC); @ksort($files, SORT_NUMERIC);//this might actually be something I would have to change, but I don't no how. As far as I can see, this part here indicates that the directories and files are sorted numerically by date. But then again I might misinterpret the "sort". Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted March 17, 2008 Share Posted March 17, 2008 learn Mysql Queries.... faster, easier, more options.... Quote Link to comment Share on other sites More sharing options...
ea7p0 Posted March 17, 2008 Author Share Posted March 17, 2008 I really tried to find a MySQL based solution to this problem and I didn't find one. The thing is this: I could store the link to a file on a file-hosting provider's page in a database and display that link on a page. No big deal. But then users cannot save directly from my page but have to go to the providerfirst. Plus, I have to go to zShare, upload the file, copy the link, go to my page, paste it there. Not precisely what I call convenient. If you can teach me a MySQL/php query that allows the following I will definitely use it: If I upload a file into a particular folder on my server, a link to the file will be posted immediately, and the files occur in alphabetical order. Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted March 18, 2008 Share Posted March 18, 2008 thats easy.... everything you were doing there can done in only a few lines of Mysql and you can upload the most recent 10 files <?php $result = mysql_query("SELECT * FROM `table` ORDER BY `name` LIMIT 10") while($row = mysql_fetch_assoc($result)){ echo '<a href ="game.php?title=';//start of link code echo $row['link']; echo '">'; echo $row['name']; echo '</a>'.'</br>';// end of link code } ?> that does it all. or to check is something was upload today... do $result = mysql_query("SELECT * FROM `table WHERE `date`= date( choose format) ORDER BY `name` ") Quote Link to comment 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.