Jump to content

[SOLVED] Listing files in a directory


wrathican

Recommended Posts

hey guys im trying to list files in a directory, the directory contains many different types of files like images, pdf's, sub directories, txt files and other php files.

what i wanna do is list the files with links to each of them, but with images i want a little thumbnail to show up next to it.

 

so far ive got this:

<?php
if ($handle = opendir('.')) {
echo "<ul>";
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
		$ext = strstr($file, '.');
		if($ext == ".gif"){
		echo "<li><a href='".$file."'><img src='".$file."' width='25'>".$file."</a></li>\n";
		}else{
            echo "<li><a href='".$file."'>$file</a></li>\n";
		}
        }
    }
    closedir($handle);
echo "</ul>";
}
?>

 

how do i get it so that my if statement can take multiple examples of $ext? like .jpg, .png and such other image files?

when i load the file i don't get the little thumbnail i 'think' ive made

also my brief says this too:

Thumbnails would be one great way of quickly seeing the contents of images, text files etc

is it possible to show a thumbnail of inside a txt file? :S

 

thanks alot

Link to comment
Share on other sites

wouldn't it be easier to user glob ???

 

<?php
echo "<ul>";
$array = array('gif','jpg','jpeg','png');
for($i = 0;$i <= 3; $i++)
{
foreach (glob("*.{$array[$i]}") as $filename)
{
	echo "<li><a href=\"{$filename}\">";
	echo "<img src=\"{$file}\"' width=\"25\" border=\"0\">";
	echo "{$filename}</a></li>\n";
}
}
echo "</ul>";
?> 

 

~ Chocopi

Link to comment
Share on other sites

Try this (worked for me):

 

<?php

echo "<ul>";

$img_extensions = array(".gif", ".png", ".jpg", ".jpeg", ".jpe", ".bmp");

$files = scandir(".");
foreach($files as $file)
{
if($file != "." && $file != "..")
{
	$ext = strtolower(strrchr($file,"."));
	$is_img = (in_array($ext, $img_extensions)) ? 1 : 0;
	echo "<li><a href='".$file."'>";
	if($is_img)
		echo "<img src='".$file."' width='25' border='0'>";
	echo $file."</a></li>\n";
}
}

echo "</ul>";

?>

 

 

 

also my brief says this too:

Quote

Thumbnails would be one great way of quickly seeing the contents of images, text files etc

is it possible to show a thumbnail of inside a txt file? :S

I guess you could read the contents of the file into a string and write it into a picture using the GD library but I dont think its worth it...

 

 

Orio.

Link to comment
Share on other sites

When it comes to previews, I use orangoo's greybox. I can do this with images, not really tried a plain text file, but might be possible, html pages will render, so plain text should not be hard. When you are checking the file extensions you probably will need to build a switch/case so the right type of link is built for greybox.

 

Just a thought...certainly not necessary.

 

http://orangoo.com/labs/GreyBox/

Link to comment
Share on other sites

thanks for your help guys.

 

ive managed to come up with this:

<?php
$ext_list = array ('.jpg', '.JPG', '.gif', '.GIF', '.png', '.PNG', '.JPEG', '.jpeg');
$list_ignore = array ('.','..');

if($handle = opendir('.')){
echo "Directories:<ul>";
while($dir = readdir($handle)) {
	if (is_dir($dir) && !in_array($dir,$list_ignore)) {
		echo '<li><a href="'.$dir.'"> '.$dir.'</a></li>';
	}
}

echo "</ul>Images:<ul>";
while($image = readdir($handle)) {
	$ext = strstr($image, '.');
	if(in_array($ext,$ext_list)){
		echo '<li><a href="'.$image.'"><img src="'.$image.'" width="25"> '.$image.'</a></li>\n';
	}
}
}
?>

but i cant get it to list the images in the directory :/

any ideas why?

Link to comment
Share on other sites

Try the ext variable as the haystack.

		if(in_array($ext_list,$ext)){
		echo '<li><a href="'.$image.'"><img src="'.$image.'" width="25"> '.$image.'</a></li>\n';
	}

--Edit

Also try:

<?php
$ext_list = array ('jpg', 'JPG', 'gif', 'GIF', 'png', 'PNG', 'JPEG', 'jpeg');
$list_ignore = array ('.','..');

if($handle = opendir('.')){
echo "Directories:<ul>";
while($dir = readdir($handle)) {
	if (is_dir($dir) && !in_array($dir,$list_ignore)) {
		echo '<li><a href="'.$dir.'"> '.$dir.'</a></li>';
	}
}

echo "</ul>Images:<ul>";
while($image = readdir($handle)) {
	$ext = strstr($image, '.');
	if(in_array($ext,'.'.$ext_list)){
		echo '<li><a href="'.$image.'"><img src="'.$image.'" width="25"> '.$image.'</a></li>\n';
	}
}
}
?>

Link to comment
Share on other sites

Try this (worked for me):

 

<?php

echo "<ul>";

$img_extensions = array(".gif", ".png", ".jpg", ".jpeg", ".jpe", ".bmp");

$files = scandir(".");
foreach($files as $file)
{
if($file != "." && $file != "..")
{
	$ext = strtolower(strrchr($file,"."));
	$is_img = (in_array($ext, $img_extensions)) ? 1 : 0;
	echo "<li><a href='".$file."'>";
	if($is_img)
		echo "<img src='".$file."' width='25' border='0'>";
	echo $file."</a></li>\n";
}
}

echo "</ul>";

?>

 

 

 

also my brief says this too:

Quote

Thumbnails would be one great way of quickly seeing the contents of images, text files etc

is it possible to show a thumbnail of inside a txt file? :S

I guess you could read the contents of the file into a string and write it into a picture using the GD library but I dont think its worth it...

 

 

Orio.

 

 

I already told you the script above, that was made by me, works exactly like you want it, so why are you ignoring it?

 

Orio.

Link to comment
Share on other sites

well, i want each type of file to list under a seperate heading.

like this:

Directories:

-D1

-D2

 

Images:

-I1

-I2

 

Other files:

-O1

-O2

 

something like that

at the moment my directories works but no images display under the images header.

and i dont get why, it looks like it should be able to do it when i look at it, but i mimght be missing something:

$ext_list = array ('.jpg', '.JPG', '.gif', '.GIF', '.png', '.PNG', '.JPEG', '.jpeg');
$list_ignore = array ('.','..');
$handle = opendir(".");

if($handle = opendir('.')){
//lists directories
echo "Directories:<ul>";
while($dir = readdir($handle)) {
	if (is_dir($dir) && !in_array($dir,$list_ignore)) {
		echo '<li><a href="'.$dir.'"> '.$dir.'</a></li>';
	}
}
//lists images
echo "</ul>Images:<ul>";
while($image = readdir($handle)) {
	$ext = strstr($image, '.');
	if(in_array($ext,$ext_list)){
		echo '<li><a href="'.$image.'"><img src="'.$image.'" width="25"> '.$image.'</a></li>\n';
	}
}
//lists all other files
echo "</ul>Other files:<ul>";
while($other = readdir($handle)) {
	$ext = strstr($other, '.');
	if(!in_array($ext,$ext_list) && !is_dir($other)){
		echo '<li><a href="'.$other.'">'.$other.'</a></li>\n';
	}
}
}

Link to comment
Share on other sites

<?php

$dirs = array();
$imgs = array();
$others = array();

$img_extensions = array(".gif", ".png", ".jpg", ".jpeg", ".jpe", ".bmp");

$files = scandir(".");
foreach($files as $file)
{
if($file != "." && $file != "..")
{
	$ext = strtolower(strrchr($file,"."));
	if(is_dir($file))
		$dirs[] = $file;
	elseif(in_array($ext, $img_extensions))
		$imgs[] = $file;
	else
		$others[] = $file;
}
}

if(count($dirs) > 0)
{
echo "<b>Directories</b><br>\n<ul>";
foreach ($dirs as $file)
	echo "<li><a href='".$file."'>".$file."</a></li>\n";
echo "</ul>\n\n<br><br>\n\n";
}
if(count($imgs) > 0)
{
echo "<b>Images</b><br>\n<ul>";
foreach ($imgs as $file)
	echo "<li><a href='".$file."'><img src='".$file."' width='25' border='0'>".$file."</a></li>\n";
echo "</ul>\n\n<br><br>\n\n";
}
if(count($others) > 0)
{
echo "<b>Other Files</b><br>\n<ul>";
foreach ($others as $file)
	echo "<li><a href='".$file."'>".$file."</a></li>\n";
echo "</ul>";
}

?>

 

 

Orio.

Link to comment
Share on other sites

Your script isn't listing the other stuff because you need to reopen the dir in each one of the three loops, because if you continue and use the same resource from the first loop its internal pointer will already be in the end, meaning you will got false right away. close the dir and reopen it, then it should work (unless you got some other error).

I think its much more efficient in my way- going over the files only once. But thats up to you- if you want to go over the entire directory 3 times, enjoy.

 

Orio.

Link to comment
Share on other sites

thanks alot for the help. now you have mentioned it i can see what the problem is!

the main reason i wanted to use mine is that i understand exactly what is going on in it. but what im going to do is sit down and study yours just to make sure i do understand it. thanks again!

Link to comment
Share on other sites

just a quick question.

what is this bit doing? i think i understand the rest:

<?php
foreach($files as $file)
{
if($file != "." && $file != "..")
{
	$ext = strtolower(strrchr($file,"."));
                //mainly wondering what this bit is
	if(is_dir($file))
		$dirs[] = $file;
	elseif(in_array($ext, $img_extensions))
		$imgs[] = $file;
	else
		$others[] = $file;
}
}
?>

 

Link to comment
Share on other sites

so i want to clear up my output a big. i want the names to appear without the extension and with capitals at the begining and replace and hyphens and underscores with spaces.

im messing with strings :D

heres what i have to start with:

//determines the name
$filename = "thIs_is-iT  for sURe.jpg";
//gets the position of the last occurence of the char
$filenamea = strrpos($filename, ".");
//displays the filename without the ext
$filenameb = substr($filename, 0, $filenamea);
//set replace array
$to_replace = array("-","_","  "," ");
//replace the chars
$filenamec = str_replace($to_replace, " ", $filenameb);
//make all lower case
$filenamed = strtolower($filenamec);

 

i do realise this is rather messy and if there is an easier way please point me towards it!

but this is only practise.

 

the only problem is now i cant find a function to turn the first letter into a capital so the name ends up like this:

"This is it for sure"

is there such a function?

Link to comment
Share on other sites

woop, thank you

 

now the only problem is that when i make this into a function and echo the names of the files it seems to cause an error and it doesnt completely list all the files and such.

well to be honest im prbably just fucking it up by not doing it right...

im not completely up on functions.. :/ which i guess is a bad thing

here my completed source code:

<?php
function changeName($file,$filenamee){
//determines the name
$filename = $file;
//gets the position of the last occurence of the char
$filenamea = strrpos($filename, ".");
//displays the filename without the ext
$filenameb = substr($filename, 0, $filenamea);
//set replace array
$to_replace = array("-","_","  "," ");
//replace the chars
$filenamec = str_replace($to_replace, " ", $filenameb);
//make all lower case
$filenamed = strtolower($filenamec);
//make first character uppercase
$filenamee = ucfirst($filenamed);

//echo($filenamea);
//echo($filenameb);
//echo($filenamec);
//echo($filenamed);
echo($filenamee);
}

$dirs = array();
$imgs = array();
$others = array();

$img_extensions = array(".gif", ".png", ".jpg", ".jpeg", ".jpe", ".bmp");

$files = scandir(".");
foreach($files as $file)
{
if($file != "." && $file != "..")
{
	$ext = strtolower(strrchr($file,"."));
	if(is_dir($file))
		$dirs[] = $file;
	elseif(in_array($ext, $img_extensions))
		$imgs[] = $file;
	else
		$others[] = $file;
}
}

if(count($dirs) > 0)
{
echo "<b>Directories</b><ul>";
foreach ($dirs as $file)
	echo "<li><a href='".$file."'>"
	changeName($file,$filenamee);
	echo "</a></li>";
echo "</ul>";
}
if(count($imgs) > 0)
{
echo "<b>Images</b><ul>";
foreach ($imgs as $file)
	echo "<li><a href='".$file."'><img src='".$file."' width='25' border='0'> ".$file."</a></li>";
echo "</ul>";
}
if(count($others) > 0)
{
echo "<b>Other Files</b><ul>";
foreach ($others as $file)
	echo "<li><a href='".$file."'>".$file."</a></li>";
echo "</ul>";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.