954,176 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with directory displayer code

Here's the situation. I created a program of sorts that allows the writer of our clubs newsletter to upload the newsletter each month to the "newsletter" folder. Then i have a php script that shows the content of the folder in an iframe on the home page so that users can download the .doc files.

The problem is that, with how the uploader php files are set up and the php directory displayer file is set up, i need to have them in the "newsletter" folder. Becouse of that, they show up in the directory listing for downloading. Does anyone know how to make the directory php script only show the .doc files and nothing esle??? This is the very last part of the program i'm building and i really need some expert advice on this. Is there a "if file is '.php', then hide this file" script i can add to the directory displaying script?

Please help. Thanks!

Curtis

capitalcitycorvairs.com
Chat-Forum-Videos-Photos

curtis_henken
Newbie Poster
5 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

Filter it with if command:

[php]$file = readdir(opendir('../newsletter/'))
// filter file
if(substr($file,-3,3) == "doc") {
// you may want to display the filesize
$fsize = filesize('../newsletter/' .$file);
echo $file." (".round($fsize/1024,2)." KB)";
}[/php]

zippee
Posting Whiz in Training
294 posts since Jan 2005
Reputation Points: 10
Solved Threads: 7
 

If you have more file types you want to display, you can use array to do it:
[php]
$file = readdir(opendir('../newsletter/'))
$ext = substr($file, -3, 3);
$accept = array('pdf','doc','xls','ppt','txt','zip');
if(in_array($ext, $accept)) {
//display file ...
}[/php]

zippee
Posting Whiz in Training
294 posts since Jan 2005
Reputation Points: 10
Solved Threads: 7
 


<?php
clearstatcache();
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != substr($PHP_SELF, -(strlen($PHP_SELF) - strrpos($PHP_SELF, "/") - 1))) {

if (filetype($file) == "dir") {
//SET THE KEY ENABLING US TO SORT
$n++;
if($_REQUEST['sort']=="date") {
$key = filemtime($file) . ".$n";
}
else {
$key = $n;
}
$dirs[$key] = $file . "/";
}
else {
//SET THE KEY ENABLING US TO SORT
$n++;
if($_REQUEST['sort']=="date") {
$key = filemtime($file) . ".$n";
}
elseif($_REQUEST['sort']=="size") {
$key = filesize($file) . ".$n";
}
else {
$key = $n;
}
$files[$key] = $file;
}
}
}
closedir($handle);
}

#USE THE CORRECT ALGORITHM AND SORT OUR ARRAY
if($_REQUEST['sort']=="date") {
@ksort($dirs, SORT_NUMERIC);
@ksort($files, SORT_NUMERIC);
}
elseif($_REQUEST['sort']=="size") {
@natcasesort($dirs);
@ksort($files, SORT_NUMERIC);
}
else {
@natcasesort($dirs);
@natcasesort($files);
}

#ORDER ACCORDING TO ASCENDING OR DESCENDING AS REQUESTED
if($_REQUEST['order']=="desc" && $_REQUEST['sort']!="size") {$dirs = array_reverse($dirs);}
if($_REQUEST['order']=="desc") {$files = array_reverse($files);}
$dirs = @array_values($dirs); $files = @array_values($files);

echo "";
if($_REQUEST['sort']!="name") {
echo "";
}
else {
if($_REQUEST['order']=="desc") {#
echo "
";
}
else {
echo "
";
}
}
echo "File
";
if($_REQUEST['sort']!="size") {
echo "";
}
else {
if($_REQUEST['order']=="desc") {#
echo "
";
}
else {
echo "
";
}
}
echo "Size
";
if($_REQUEST['sort']!="date") {
echo "";
}
else {
if($_REQUEST['order']=="desc") {#
echo "
";
}
else {
echo "
";
}
}
echo "Date Modified
";

$arsize = sizeof($dirs); for($i=0;$i<$arsize;$i++) { echo "\t"; echo "\t\t\"Directory\""; echo "\t\t" . $dirs[$i] . ""; echo "\t\t-"; echo "\t\t" . date ("M d Y h:i:s A", filemtime($dirs[$i])) . ""; echo "\t"; }

$arsize = sizeof($files); for($i=0;$i<$arsize;$i++) { switch (substr($files[$i], -3)) { case "jpg": $img = "jpg.gif"; break; case "gif": $img = "gif.gif"; break; case "zip": $img = "zip.gif"; break; case "png": $img = "png.gif"; break; case "avi": $img = "move.gif"; break; case "mpg": $img = "move.gif"; break; default: $img = "what.gif"; break; } echo "\t\r\n"; echo "\t\t\"Directory\"\r\n"; echo "\t\t" . $files[$i] . "\r\n"; echo "\t\t" . round(filesize($files[$i])/1024) . "KB\r\n"; echo "\t\t" . date ("M d Y h:i:s A", filemtime($files[$i])) . "\r\n"; echo "\t\r\n"; } echo "Directory Listing Script. © 2003-2004 Ash Young"; ?>

curtis_henken
Newbie Poster
5 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

If not mistaken, should be after this lot, and before print the values.
[PHP]#ORDER ACCORDING TO ASCENDING OR DESCENDING AS REQUESTED
if($_REQUEST['order']=="desc" && $_REQUEST['sort']!="size") {$dirs = array_reverse($dirs);}
if($_REQUEST['order']=="desc") {$files = array_reverse($files);}
$dirs = @array_values($dirs); $files = @array_values($files);
[/PHP]

zippee
Posting Whiz in Training
294 posts since Jan 2005
Reputation Points: 10
Solved Threads: 7
 

Hey Zippee,

I inserted your code into the script and nothing changed. Is there any way you could try this script with your code in it and let me know what you find?

Thanks,

Curtis

curtis_henken
Newbie Poster
5 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You