PDA

View Full Version : echo filename without file extension



toille
December 25th, 2004, 06:32 PM
my focus is toward the top of the code. im trying to list all of my files in a folder (.txt files), but without the ".txt", and echo them out as links. Can anyone help me revise my code? you can see it in action at http://americanliterature.hebronix.com/episodes.php. the different episodes are linked by ID in the url. and the text files contain the info. i realized i would rather use mysql, but i am really tired and have been working on this for a while. sorry if this doesnt make sense, i am really really tired.


<?php include('header.php');


//define the path as relative
$path = "/home/roche/americanliterature.hebronix.com/moviedata";

//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");

// strip txt from name an assign it as the key value
$find = ".txt";
$pos = strpos($file, $find);
$keyval = substr($file, 0, $pos);

echo "<span class='small'>Episodes:</span> ";

//running the while loop
while ($file = readdir($dir_handle))
{
if($file!="." && $file!="..")
echo "<a href='episodes.php?id=$file' class='small'>$file</a>, ";
}

//closing the directory
closedir($dir_handle);


if (isset($HTTP_GET_VARS['id']))
{
if ($HTTP_GET_VARS['id'] != '')
{

$filename = "./moviedata/" . $_GET['id'] . ".txt";
$fp = fopen($filename,'r') or die("Please Select An Episode");
$contents = fread($fp, filesize($filename));
fclose($fp);

$contents = trim($contents);
$data = explode("|", $contents);

echo "<table width='558' cellpadding='0' cellspacing='0' background='images/episodebg.gif'>
<tr><td width='558' height='40' background='images/episodetop.gif'><img src='images/ep\n" . $_GET['id'] . "\n.gif'></td></tr>

<tr><td>
<table cellpadding='0' cellspacing='0' width='100%' height='100%'>
<tr valign='top'>
<td><img src='images/left.gif'><img src='images/photo.gif'></td>

<td><img src='images/viewtrailer.gif' border='0'><img src='images/dotdot.gif'><img src='images/download.gif' border='0'><img src='images/rightcorner.gif'><br><br>
<img src='images/featuring.gif'><br>
<table><td width='7'></td><td width='380'>\n" . $data[0] . "\n</td></table><br>
<img src='images/description.gif'><br>
<table><td width='7'></td><td width='380'>\n" . $data[1] . "\n</td></table></td>
</tr>
</table>
</td></tr>

<tr><td><img src='images/episodebottom.gif'></td></tr>
</table>";

} else {
echo "<br>Please Select An Episode";
}
}

include('footer.php'); ?>

toille
December 25th, 2004, 10:21 PM
alright guys, with a whole day infront of the computer i figured it out. its not a big issue for me, but if anyone knows how to get rid of that last comma itd be greatly appreciated :hugegrin: ... oh, and you can see what i mean here http://americanliterature.hebronix.com/episodes.php


<?php include('header.php');


//define the path as relative
$path = "/home/roche/americanliterature.hebronix.com/moviedata";

//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");

echo "<span class='small'>Episodes:</span> ";

//running the while loop
while ($file = readdir($dir_handle))

{
$file = str_replace(".txt", "", $file);
if($file!="." && $file!="..")
echo "<a href='episodes.php?id=$file' class='small'>$file</a>, ";
}

//closing the directory
closedir($dir_handle);


if (isset($HTTP_GET_VARS['id']))
{
if ($HTTP_GET_VARS['id'] != '')
{

$filename = "./moviedata/" . $_GET['id'] . ".txt";
$fp = fopen($filename,'r') or die("Please Select An Episode");
$contents = fread($fp, filesize($filename));
fclose($fp);

$contents = trim($contents);
$data = explode("|", $contents);

echo "<table width='558' cellpadding='0' cellspacing='0' background='images/episodebg.gif'>
<tr><td width='558' height='40' background='images/episodetop.gif'><img src='images/ep\n" . $_GET['id'] . "\n.gif'></td></tr>

<tr><td>
<table cellpadding='0' cellspacing='0' width='100%' height='100%'>
<tr valign='top'>
<td><img src='images/left.gif'><img src='images/poster\n" . $_GET['id'] . "\n.gif' width='146' height='153'></td>

<td><a href='trailer.php?id=\n" . $_GET['id'] . "\n'><img src='images/viewtrailer.gif' border='0'></a><img src='images/dotdot.gif'><a href='episode\n" . $_GET['id'] . "\n.zip'><img src='images/download.gif' border='0'></a><img src='images/rightcorner.gif'><br><br>
<img src='images/featuring.gif'><br>
<table><td width='7'></td><td width='380'>\n" . $data[0] . "\n</td></table><br>
<img src='images/description.gif'><br>
<table><td width='7'></td><td width='380'>\n" . $data[1] . "\n</td></table></td>
</tr>
</table>
</td></tr>

<tr><td><img src='images/episodebottom.gif'></td></tr>
</table>";

} else {
echo "<br>Please Select An Episode";
}
}

include('footer.php'); ?>

Jerryscript
December 26th, 2004, 05:57 AM
Try something like:

$comma=false;
while ($file = readdir($dir_handle)) {
$file = str_replace(".txt", "", $file);
if($file!="." && $file!=".."){
echo "<a href='episodes.php?id=$file' class='small'>$file</a>";
if($comma){echo ",";}else{$comma=true;}
}
}
Happy Holidays :)