Directory listing with file info

This is a discussion on Directory listing with file info within the PHP Language forums, part of the PHP Programming Forums category; I'd like to make a directory listing where instead of the entire filename I need it to show the ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-16-2004
Kim Jensen
 
Posts: n/a
Default Directory listing with file info

I'd like to make a directory listing where instead of the entire
filename I need it to show the filename minus the extention and get the
value of charname= in the file itself.

I've been told that I had to turn the directory listing into an array
and then use "foreach (array as item)" to go through and open each file
but I've tried several different approaches and I just can't get it to work.

I've been able to make it list the directory in order using this script
but after that I'm lost.

<?
$list = Array();
$handle = opendir('testdir/.');
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$list[] = ($file);
}
}
closedir($handle);
sort ($list);
reset ($list);

while (list ($key, $val) = each ($list)) {
echo "<a href=test.php?name=$val>$val</a><br>";

}
?>

Here I'd like the $val after the name= to be just the filename without
the extention (all files in the directory are txt files) and then the
second $val which it shows in the list to be the value of the line
charname= in the txt files themselves.

Anybody have an idea what I should do?

Kim Jensen

Reply With Quote
  #2 (permalink)  
Old 11-16-2004
Erich Musick
 
Posts: n/a
Default Re: Directory listing with file info

Try this:

<?
$list = Array();
$handle = opendir('../');
while (false !== ($file = readdir($handle))) {

$fileparts = explode(".",$file);
if (count($fileparts) > 1) {
array_pop($fileparts);
}
$file = join(".",$fileparts);

if ($file != "." && $file != ".." && $file != "") {
$list[] = $file;
}
}
closedir($handle);
sort ($list);
reset ($list);

while (list ($key, $val) = each ($list)) {
echo "<a href=test.php?name=$val>$val</a><br />";

}

?>

Erich Musick

Kim Jensen wrote:
> I'd like to make a directory listing where instead of the entire
> filename I need it to show the filename minus the extention and get the
> value of charname= in the file itself.
>
> I've been told that I had to turn the directory listing into an array
> and then use "foreach (array as item)" to go through and open each file
> but I've tried several different approaches and I just can't get it to
> work.
>
> I've been able to make it list the directory in order using this script
> but after that I'm lost.
>
> <?
> $list = Array();
> $handle = opendir('testdir/.');
> while (false !== ($file = readdir($handle))) {
> if ($file != "." && $file != "..") {
> $list[] = ($file);
> }
> }
> closedir($handle);
> sort ($list);
> reset ($list);
>
> while (list ($key, $val) = each ($list)) {
> echo "<a href=test.php?name=$val>$val</a><br>";
>
> }
> ?>
>
> Here I'd like the $val after the name= to be just the filename without
> the extention (all files in the directory are txt files) and then the
> second $val which it shows in the list to be the value of the line
> charname= in the txt files themselves.
>
> Anybody have an idea what I should do?
>
> Kim Jensen
>

Reply With Quote
  #3 (permalink)  
Old 11-16-2004
Pedro Graca
 
Posts: n/a
Default Re: Directory listing with file info

Kim Jensen wrote:
> while (list ($key, $val) = each ($list)) {
> echo "<a href=test.php?name=$val>$val</a><br>";
>
> }
>
> Here I'd like the $val after the name= to be just the filename without
> the extention (all files in the directory are txt files)


echo '<a href="test.php?name=', substr($val, -4), ">$val</a><br";

> and then the
> second $val which it shows in the list to be the value of the line
> charname= in the txt files themselves.


Are you really sure you want to do that?
You have top open each and every file, read/print its contents, and close
the file. It might be a lot slow!


/* needs error-checking for fopen() and fgets() */
foreach ($list as $val) {
$f = fopen($val);
while (!feof(f)) {
$line = fgets($f);
if (preg_match('/^charname=(.*)$/', $line, $matches)) {
echo '<a href="test.php?name=', urlencode(substr($val, -4)),
'">', $matches[1], '</a><br>';
break; /* leave inner while() */
}
}
fclose($f);
}
--
Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
== ** ## !! !! ## ** ==
TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
bypass the spam filter. I will answer all pertinent mails from a valid address.
Reply With Quote
  #4 (permalink)  
Old 11-16-2004
Erich Musick
 
Posts: n/a
Default Re: Directory listing with file info

This method, though, assumes that all extensions are exactly three
characters long.

What about extensions such as pl, py, h, among others?



Pedro Graca wrote:
> Kim Jensen wrote:
>
>>while (list ($key, $val) = each ($list)) {
>> echo "<a href=test.php?name=$val>$val</a><br>";
>>
>>}
>>
>>Here I'd like the $val after the name= to be just the filename without
>>the extention (all files in the directory are txt files)

>
>
> echo '<a href="test.php?name=', substr($val, -4), ">$val</a><br";
>
>
>>and then the
>>second $val which it shows in the list to be the value of the line
>>charname= in the txt files themselves.

>
>
> Are you really sure you want to do that?
> You have top open each and every file, read/print its contents, and close
> the file. It might be a lot slow!
>
>
> /* needs error-checking for fopen() and fgets() */
> foreach ($list as $val) {
> $f = fopen($val);
> while (!feof(f)) {
> $line = fgets($f);
> if (preg_match('/^charname=(.*)$/', $line, $matches)) {
> echo '<a href="test.php?name=', urlencode(substr($val, -4)),
> '">', $matches[1], '</a><br>';
> break; /* leave inner while() */
> }
> }
> fclose($f);
> }


--
In Christ,

Erich Musick
http://erichmusick.com

In the same way, let your light shine before others, so that they may
see your good works and give glory to your Father who is in heaven. --
Matthew 5:16 NKJV
Reply With Quote
  #5 (permalink)  
Old 11-16-2004
Kim Jensen
 
Posts: n/a
Default Re: Directory listing with file info

Pedro Graca wrote:
> Are you really sure you want to do that?
> You have top open each and every file, read/print its contents, and close
> the file. It might be a lot slow!


The problem is that the filenames themselves even if they contain the
name I'm looking for aren't really pleasing to the eye as links.

I'd rather have links that read:

Angelina Jolie
Gwynneth Paltrow
Jude Law
Michael Gambon
Sir Lawrence Olivier

than

angelinajolie
gwynnethpaltrow
judelaw
lawrenceolivier
michaelgambon

Sorry, just got home from watching Sky Captain :)

Kim Jensen

Reply With Quote
  #6 (permalink)  
Old 11-16-2004
Tenzel Kim
 
Posts: n/a
Default Re: Directory listing with file info

Kim Jensen wrote:
> Pedro Graca wrote:
>
>> Are you really sure you want to do that?
>> You have top open each and every file, read/print its contents, and close
>> the file. It might be a lot slow!

>
>
> The problem is that the filenames themselves even if they contain the
> name I'm looking for aren't really pleasing to the eye as links.
>
> I'd rather have links that read:
>
> Angelina Jolie
> Gwynneth Paltrow
> Jude Law
> Michael Gambon
> Sir Lawrence Olivier
>
> than
>
> angelinajolie
> gwynnethpaltrow
> judelaw
> lawrenceolivier
> michaelgambon
>
> Sorry, just got home from watching Sky Captain :)


Just forgot something. If I was just now creating the whole thing I
could easily use the first examples as file names, but unfortunately I
already have thousands of links using the second type of file names so
going back and changing all of them manually or even by global
search/replace would take ages.

Kim Jensen

Reply With Quote
  #7 (permalink)  
Old 11-16-2004
Pedro Graca
 
Posts: n/a
Default Re: Directory listing with file info

Erich Musick top-posted (and I changed the format):
> Pedro Graca wrote:
>> Kim Jensen wrote:
>>
>>>Here I'd like the $val after the name= to be just the filename without
>>>the extention (all files in the directory are txt files)


^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

>> echo '<a href="test.php?name=', substr($val, -4), ">$val</a><br";

>
> This method, though, assumes that all extensions are exactly three
> characters long.


Yes, as the OP stated.

> What about extensions such as pl, py, h, among others?


In this case, it didn't seem necessary to make a generic function -- but
I agree that would have been a better option from the start :)

--
Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
== ** ## !! !! ## ** ==
TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
bypass the spam filter. I will answer all pertinent mails from a valid address.
Reply With Quote
  #8 (permalink)  
Old 11-16-2004
Kim Jensen
 
Posts: n/a
Default Re: Directory listing with file info

Pedro Graca wrote:
> Are you really sure you want to do that?
> You have top open each and every file, read/print its contents, and close
> the file. It might be a lot slow!


About the slowness. Couldn't it be done so that it only took the files
starting with a specific letter?

So that if I want to see a listing of the files starting with A I'd open
dir.php?letter=a

Kim Jensen

Reply With Quote
  #9 (permalink)  
Old 11-16-2004
Erich Musick
 
Posts: n/a
Default Re: Directory listing with file info



Pedro Graca wrote:
> Erich Musick top-posted (and I changed the format):
>
>>Pedro Graca wrote:
>>
>>>Kim Jensen wrote:
>>>
>>>
>>>>Here I'd like the $val after the name= to be just the filename without
>>>>the extention (all files in the directory are txt files)

>
>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
>
>>>echo '<a href="test.php?name=', substr($val, -4), ">$val</a><br";

>>
>>This method, though, assumes that all extensions are exactly three
>>characters long.

>
>
> Yes, as the OP stated.
>
>


My bad - failed to notice that :)

>>What about extensions such as pl, py, h, among others?

>
>
> In this case, it didn't seem necessary to make a generic function -- but
> I agree that would have been a better option from the start :)
>


--
In Christ,

Erich Musick
http://erichmusick.com

In the same way, let your light shine before others, so that they may
see your good works and give glory to your Father who is in heaven. --
Matthew 5:16 NKJV
Reply With Quote
  #10 (permalink)  
Old 11-16-2004
Erich Musick
 
Posts: n/a
Default Re: Directory listing with file info

Yeah...that's a possibility


Kim Jensen wrote:
> Pedro Graca wrote:
>
>> Are you really sure you want to do that?
>> You have top open each and every file, read/print its contents, and close
>> the file. It might be a lot slow!

>
>
> About the slowness. Couldn't it be done so that it only took the files
> starting with a specific letter?
>
> So that if I want to see a listing of the files starting with A I'd open
> dir.php?letter=a
>
> Kim Jensen
>


--
In Christ,

Erich Musick
http://erichmusick.com

In the same way, let your light shine before others, so that they may
see your good works and give glory to your Father who is in heaven. --
Matthew 5:16 NKJV
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:40 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0