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 15 2479
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
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.
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
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
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
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.
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
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
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
[ (-: Tenzel Kim Jensen? ]
Tenzel Kim wrote: Kim Jensen wrote: I'd rather have links that read:
Angelina Jolie
than
angelinajolie
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.
Write a script to do that for you!
<?php
$oldfiles_dir = 'old/';
$newfiles_dir = 'new/';
foreach ($list as $val) {
/* get $charname :: fopen(), fgets(), fclose() */
copy($oldfiles_dir . $val, $newfiles_dir . $charname);
}
?>
And presto! :-)
--
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.
Erich Musick wrote: Pedro Graca wrote: Erich Musick top-posted (and I changed the format):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 :)
What about
filename.with.a.lot.of.dots
and
filename_without_dots
?
:-)
The generic function would have to deal with them!
--
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.
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
Or you could make a 'index' file and only open that one. Its contents
would be something like:
angelinajolie.txt Angelina Jolie
gwynnethpaltrow.txt Gwynneth Paltrow
judelaw.txt Jude Law
lawrenceolivier.txt Sir Lawrence Olivier
michaelgambon.txt Michael Gambon
Or use a database.
Of course these options assume the 'index' file and the database would
be updated for every new file ... (and deletions and changes).
--
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.
The code i posted earlier should work with that...
Pedro Graca wrote: Erich Musick wrote:
Pedro Graca wrote:
Erich Musick top-posted (and I changed the format):
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 :)
What about filename.with.a.lot.of.dots and filename_without_dots ?
:-)
The generic function would have to deal with them!
--
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
Pedro Graca wrote: [ (-: Tenzel Kim Jensen? ]
Heh. That's the problem with having a split personality. Sometimes you
forget which is which :)
Kim Jensen or was that Tenzel Kim???
Pedro Graca wrote: Or use a database.
I was trying to avoid using a database for now, as I know that if I went
ahead with that, it would end up being so huge in scope that I'd never
finish it :)
Of course these options assume the 'index' file and the database would be updated for every new file ... (and deletions and changes).
I have been considering the index file approach. However, considering
the speed problem is mainly a problem I don't want my visitors to
encounter making the slow "grab the info from all the files" version
could be done on my harddrive and the output of the script I could just
copy and paste into an index page. That way the slow stuff will be done
behind the scenes.
Maybe that's the best option. Hadn't considered that until just now.
Kim Jensen This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Hal Vaughan |
last post by:
I've seen install programs that search a hard drive for previous instances
of a program, or to find installs of other needed programs.
I need to search a hard drive for any installations of...
|
by: Dean |
last post by:
Hi
I've got a question relating to using Javascript on an Intranet. I
have a directory with a list of files in the format week36.xls,
week37.xls
and I want to write a script that will scan...
|
by: Vasilis Serghi |
last post by:
I would like to make a call to the OS to give me a listing of the files in
the current directory. How can i do this in a console environment?
I have tried system("dir");, but I'm not sure how to...
|
by: Martin |
last post by:
Hi,
I have implemeted a recursive function that basically iterate throught the
entire file system and prints out the entire path of each directory that it
come across.
The only problem is the...
|
by: Tim_Mac |
last post by:
this could be seen as petty...
but i noticed if you browse a folder on a .net 2 virtual directory, you
get the directory listing. but the font styles are set to 8 point
verdana which is...
|
by: gil |
last post by:
Is it possible to prevent a browser from listing the entire contents of
a folder?
The site, is hosted on my ISP with the following layout-
site/ "user name from ISP"
pagefile (dir)...
|
by: Craig Vermeer |
last post by:
Hi All,
I have a program that's using the file system as a queuing mechanism,
and it's consuming an inordinate amount of CPU time when the file system
queue gets all that large (any more than a...
|
by: dougawells |
last post by:
Hi -
I'm hoping for help with the auto-generation of a hyperlinked listing
of all files in a directory. The server I use does not auto-generate
this. So, when someone comes to this directory and...
|
by: techusky |
last post by:
I have a *very* simple script written that displays the directory
listing of the current working directory, but I am having some
difficulty when I try to change folders. Basically, I have my $dir...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |