Jump to content

[SOLVED] Listing files in a directory


wrathican

Recommended Posts

right i see.

now i have a problem when using it though. i *think* im using it correctly heres the difference:

before the function:

listyes.gif

after using the function:

listnon.gif

 

any idea why its doing this?

heres the code im using when using the function:

<?php
function changeName($file)
{
$path_parts = pathinfo($file);

return ucfirst(strtolower(str_replace(array('-', '_', '  '), ' ', $path_parts['filename'])));
}

$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."'>";
	echo changeName($file);
	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'> ";
	echo changeName($file);
	echo "</a></li>";
echo "</ul>";
}
if(count($others) > 0)
{
echo "<b>Other Files</b><ul>";
foreach ($others as $file)
	echo "<li><a href='".$file."'>";
	echo changeName($file);
	echo "</a></li>";
echo "</ul>";
}
?>

Link to comment
Share on other sites

EDIT- Read next post, not this!!

 

I guess you are using PHP < 5.2.0

Use this function instead:

 

<?php

function changeName($file)
{
$file = substr($file, 0, -(strlen(strrchr($file, "."))));
return ucfirst(strtolower(str_replace(array('-', '_', '  '), ' ', $file)));
}

?>

 

 

Orio.

Link to comment
Share on other sites

lol I found the problem... Curly braces.

When I orginally made the script each foreach had one line so I omitted the curly braces. But after you add more lines you have put them back.

Do it this way:

 

<?php
function changeName($file)
{
$cut = -(strlen(strrchr($file, ".")));
if($cut != 0)
	$file = substr($file, 0, $cut);
return ucfirst(strtolower(str_replace(array('-', '_', '  '), ' ', $file)));
}

$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."'>";
	echo changeName($file);
	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'> ";
	echo changeName($file);
	echo "</a></li>";
}
echo "</ul>";
}
if(count($others) > 0)
{
echo "<b>Other Files</b><ul>";
foreach ($others as $file)
{
	echo "<li><a href='".$file."'>";
	echo changeName($file);
	echo "</a></li>";
}
echo "</ul>";
}
?>

 

I also made it work with php < 5.2.0

 

 

Orio.

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.