i have a script that makes links out of files found in a specified directory. it pretties up the links, too. my problem is that i want to take out the extension, but then when i go to include the content specified by the link, i don't know what extension to use. i'd like to be able to include php and htm and html files, etc... but can i discretely pass the extension on to the include statement somehow? i can't see how it can be done, yet it seems so simple. anyone have any ideas?function autoLink ($dir)
{
if ($handle = opendir("content/"."$dir")) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$file_array[] = $file;
}
}
closedir($handle);
}
// start loop
foreach ($file_array as $file) {
$ext = strrchr($file, "."); // find the extension
$link_url = substr($file, 0, -strlen($ext));// strip it out from the '?var=' URL
$link_text = str_replace(array("_","-"),array(" ","'"), $file);// now do special chars
$link_text = substr($link_text, 0, -strlen($ext));// strip out ext from link text
echo "<a href=http://www.htmlforums.com/archive/index.php/\"?section=$dir&article=$link_url\">$link_text$ext</a><br />\n";
}
}(to clarify: because $ext potentially has a different value through each pass of the loop... after say, 5 links, $ext will only mean whatever the last value it had was (say ".html"). yet i want to be able toinclude($article$ext);based one whatever link someone clicks on. note that it is important to me that the link stay as pretty as possible; i.e., ?article=my_article vs ?article=my_article.htm)Firstly, I would say that when linking to articles etc you should consider using:header("Location: <!-- m --><a class="postlink" href="http://">http://</a><!-- m -->".$article.$ext);However, in direct response to your question: your example is pretty much correct except you appear to have forgotten the concatenation operand (.)include($article.$ext)In this case the $article might contain "TyingShoeLace" and the $ext would contain something like ".htm"err, no, i think i wasn't being clear. let's forget about concatenation here... it's not the issue at all, in fact it works to some degree regardless, even if it is incorrect. and i will fix that presently. however,...
what happens is this:
the function loops through a specified directory, and makes links out of all the files.
i've got it set up to make a link out of any type of file, and the URL (of the ?page=name variety) has teh extension lopped off, to increasify the prettiness of the URL, if you get my meaning.
then, when someone clicks on a link, the page reloads with the specified file included... only that doesn't happen, because when $ext returns five different file types, e.g.,
file1.htm
file2.html
file3.php
file4.inc
file5.txt
the only thing that $ext is going to return is the last value it had... in this case, .txt, even if it was the second link that was clicked on (file2.html) - here's the part that includes the clicked-on link:
if (!isset($article) || empty($article)) {
include("_basic.php");
}
else {
include("content/$setsection/$article$ext");
}ok. here's what i'm going to do. it's quick and dirty, and lacks finesse in a big, big way, but it works, so i'm going with it. if anybody has a better solution, feel free to post it!
if (!isset($article) || empty($article)) {
include("_basic.php");
}
else {
if(file_exists("content/$setsection/"."$article".".php")){
include("content/$setsection/"."$article".".php");
}
elseif(file_exists("content/$setsection/"."$article".".inc")){
include("content/$setsection/"."$article".".inc");
}
elseif(file_exists("content/$setsection/"."$article".".html")){
include("content/$setsection/"."$article".".html");
}
elseif(file_exists("content/$setsection/"."$article".".htm")){
include("content/$setsection/"."$article".".htm");
}
elseif(file_exists("content/$setsection/"."$article".".txt")){
include("content/$setsection/"."$article".".txt");
}
else {
include("_basic.php");
}
}
{
if ($handle = opendir("content/"."$dir")) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$file_array[] = $file;
}
}
closedir($handle);
}
// start loop
foreach ($file_array as $file) {
$ext = strrchr($file, "."); // find the extension
$link_url = substr($file, 0, -strlen($ext));// strip it out from the '?var=' URL
$link_text = str_replace(array("_","-"),array(" ","'"), $file);// now do special chars
$link_text = substr($link_text, 0, -strlen($ext));// strip out ext from link text
echo "<a href=http://www.htmlforums.com/archive/index.php/\"?section=$dir&article=$link_url\">$link_text$ext</a><br />\n";
}
}(to clarify: because $ext potentially has a different value through each pass of the loop... after say, 5 links, $ext will only mean whatever the last value it had was (say ".html"). yet i want to be able toinclude($article$ext);based one whatever link someone clicks on. note that it is important to me that the link stay as pretty as possible; i.e., ?article=my_article vs ?article=my_article.htm)Firstly, I would say that when linking to articles etc you should consider using:header("Location: <!-- m --><a class="postlink" href="http://">http://</a><!-- m -->".$article.$ext);However, in direct response to your question: your example is pretty much correct except you appear to have forgotten the concatenation operand (.)include($article.$ext)In this case the $article might contain "TyingShoeLace" and the $ext would contain something like ".htm"err, no, i think i wasn't being clear. let's forget about concatenation here... it's not the issue at all, in fact it works to some degree regardless, even if it is incorrect. and i will fix that presently. however,...
what happens is this:
the function loops through a specified directory, and makes links out of all the files.
i've got it set up to make a link out of any type of file, and the URL (of the ?page=name variety) has teh extension lopped off, to increasify the prettiness of the URL, if you get my meaning.
then, when someone clicks on a link, the page reloads with the specified file included... only that doesn't happen, because when $ext returns five different file types, e.g.,
file1.htm
file2.html
file3.php
file4.inc
file5.txt
the only thing that $ext is going to return is the last value it had... in this case, .txt, even if it was the second link that was clicked on (file2.html) - here's the part that includes the clicked-on link:
if (!isset($article) || empty($article)) {
include("_basic.php");
}
else {
include("content/$setsection/$article$ext");
}ok. here's what i'm going to do. it's quick and dirty, and lacks finesse in a big, big way, but it works, so i'm going with it. if anybody has a better solution, feel free to post it!
if (!isset($article) || empty($article)) {
include("_basic.php");
}
else {
if(file_exists("content/$setsection/"."$article".".php")){
include("content/$setsection/"."$article".".php");
}
elseif(file_exists("content/$setsection/"."$article".".inc")){
include("content/$setsection/"."$article".".inc");
}
elseif(file_exists("content/$setsection/"."$article".".html")){
include("content/$setsection/"."$article".".html");
}
elseif(file_exists("content/$setsection/"."$article".".htm")){
include("content/$setsection/"."$article".".htm");
}
elseif(file_exists("content/$setsection/"."$article".".txt")){
include("content/$setsection/"."$article".".txt");
}
else {
include("_basic.php");
}
}