PHP-gurus: I need your help!
- Started
- Last post
- 22 Responses
- schjetne
Is it possible to make PHP list all files in a specific directory? Yes, of course this is possible, on to the real question:
Is it possible to use PHP to list the titles of all files in a specific directory, and at the same time link to them? And if so, how do you do this?
I'm making a site where the client can use the new Macromedia program Contribute to update the content, only I don't want them to have to make changes in the menu every time they make a new page, I want the menu to update the page list automagically.
It would be great if some of you knew the answer to this.
- mike0
yes you can however, you would come to find that there's a large overhead having to open and read all of the files each time to render a menu, especially when there can potentially be x number of users / concurrent users. I would recommend naming the files with a convention that could be used to render the menu... like Home-pg.html would render as home and About_Us-pg.html would render as About Us... this way you can just grab the files and then replace their names for display without having to open each of them... make sense?
- schjetne0
Yes indeed. Now if I only knew how I could do this with only limited php skills...
Any sites that have readymade scripts like this? I'm primarily a print and flash designer, so this is kinda way out there for me.
- mike0
are these the type of results you are looking for?
http://www.blather.org/misc/menu…
if so I can sent the script over and you can modify it from there to suit your needs...
- schjetne0
Yeah that's basically it. I got it to show the files, and I managed to strip away the . and .. from the list, but I have no idea how I should strip away underscores and prefixes.
I'd appreciate it alot if you send that script over. Thank you, mike!
- schjetne0
I've come this far so far:
- mike0
check your email jan.
- schjetne0
Thanks, mike! It looks a lot different than the stuff I wrote myself. I don't know if it's interesting at all, but here is my script:
if ($dir = @opendir("mooh")) {
while (($file = readdir($dir)) !== false) {
if($file=="." or $file=="..") continue;
echo "";
print(str_replace("_"," ",$file));
echo " ";
}
closedir($dir);
}This only strips the slashes and replaces them with spaces, but it's a start.
What do you think? Am I on to something?
I figured that since I have to learn this some time, I'll try some on my own before using your script.
The url to my work file is http://www.schjetne.net/test_rea… , if you want to see the result.
- schjetne0
Now how would I go about to tell the str_replace to replace underscores with spaces AND prefixes with nothing?
- mike0
yeah jan, it's basically the same... but by capturing the items in an array and then grabbing that array through a function call you have more flexibility with the data in use. you can add several conditions to match whatever it is you would have for potential file names. at some point, you might want to use regular expression matches to simplify the absolute conditions.
- schjetne0
I read your entire post, and didn't understand anything of it. Hm... This php stuff is hard. I'll try some more. I basically need a quick thing, and I need it done by tomorrow, so if I don't manage to make it myself, I'll just use yours.
Is it possible in php to strip all prefixes from the output? Right now, I use
str_replace(array('_', '.html', '.gif', '.jpg', '.swf')," ",$file)
But that doesn't strip .php, .txt, etc etc...
- mike0
In the file i sent I extracted the file extension and then replaced it later... you could add several extensions in there for valid file types if you wanted to... but having the extension makes it simple to replace... your file naming convention is going to dictate what you need to do in order to strip the filename to its base name.
you can also assign two arrays, each with a set of regular expressions for replacement conditions... then call:
$sResult = preg_replace ($aBefore, $aAfter, $sArg);
$sArg being the value you want to search and replace on, $aBefore being the array of regex matches and $aAfter being the replacement for theose key-mapped matches. the result gets assigned to $sResult.
is that confusing as hell?
- schjetne0
Yes. All I wanted to do was strip file prefixes... Yeah, wow, this certainly is confusing. I'm a _designer_ dammit, not a programmer.
Damn the Man!
Seriously, though, is there _anything at all_ I can put into str_replace to make it replace ALL prefixes with nothing?
- mike0
you would need to do a regular expression match... or, if you know that all prefixes are a certain length from the begining of the file name then you could substr the string and replace the result with nothing...
$sName = str_replace(substr($sStr, 0, 4), "", $sStr);
that should replace the first four characters of the file name wil nothing...
0 is the statrt position and 4 is the length...
http://www.php.net/manual/en/fun…
can you show me an example of what the string looks like and what you are trying to replace in each one?
- schjetne0
Of course I'm talking about suffixes(extensions). Sorry, mike. I didn't mean to confuse you.
- schjetne0
I found the suffix like this:
$ext = strrchr($file,'.');
and removed it like this:
str_replace(array('_', $ext)," ",$file)
- mike0
that works, but in the event that your file name has a dot in it other than the extension you will have a problem...
this works nicely to get the extension and ensures that it's the last .xxx in the name...
$sExt = array_pop(explode(".", $sFile));
explode breaks the string into array parts based on the delimiter and array_pop grabs the last item in the array index...
so if you file had a name like drunken.party.shot.jpg it would ensure that $sExt was assigned jpg
- schjetne0
That works with my script too.
http://www.schjetne.net/test_rea…
The last file on the list is called w.ood_sl.ats.jpg, and is displayed as w.ood sl.ats ...
- schjetne0
So now I have just removed all directories from the list. Next step should be to make the directories show on top, before the filelist... Any ideas?
- phlojonaut0
mike and schjetne
if possible, could you please post those php scripts for me too ?
I would like to see how you structured the code....
I'm not a very advanced php programer, but I'm trying to leanr, and I'm begining to understand.
Because I read the entire post, I would like if possible, to see the final code, or whatever each one of you has as the latest code..
thanks