PHP: strip file extension from a directory listing?

Sam

New member
Hello all,

I'm learning PHP and have created a script to display the contents of a folder. I want to strip the file extension from the listing but am not having much luck.

an anyone help me out?

This is what I have so far:
<?php

include 'header.php';

//Define directory to display
$url = './upload_text/';

//Define URL
$handle = opendir ($url);

//Loop to display all images
while (false !== ($file = readdir($handle))){
if($file != "." && $file != ".." && $file != basename(__FILE__)){
echo '<a href="./upload_text/'.$file.'">'.$file.'</a><br />';}
}

include 'header.php';

?>
 
There is two ways to do it, the hard way... or the easy way. Because you say you are a beginner with PHP, I will tell you a simple 'easy' method of doing this.
If you do not know the PHP attribute "explode", I suggest you read this page: http://www.tizag.com/phpT/php-string-explode.php
Explode the URL by the periods. Since the File extension is ALWAYS the last after a url (ie, .html; "html" is always last).
Now your URL is in an array, and your .html, for example, is the last value in the array, while the file name is the second to last:
<?
$fileName = explode("." , $file);
?>
To find the length of an array, use the sizeof() PHP attribute.
This will give you the last number able to be used, which will be the extension. We want the one before that (the file name) so we just subtract 1, however, sizeof starts at 1, arrays start at 0, so you really need to subtract 2; 1 to bring it to array count, and another to specify the file name.
<?
$fileName = explode(".",$file);
$fileName = $fileName[sizeof($fileName)-2];
?>
So if $fileName's array had 4 parts inside, (0-3), sizeof($fileName) will equal 4. sizeof($fileName) - 2 equals 2. $fileName[2] should equal the filename.
Good Luck!
If you need further assistance/advice, feel free to contact me.
NOTE: I did not test this script above, well, because I do not have the same script as you, nor the same directories.
 
There is two ways to do it, the hard way... or the easy way. Because you say you are a beginner with PHP, I will tell you a simple 'easy' method of doing this.
If you do not know the PHP attribute "explode", I suggest you read this page: http://www.tizag.com/phpT/php-string-explode.php
Explode the URL by the periods. Since the File extension is ALWAYS the last after a url (ie, .html; "html" is always last).
Now your URL is in an array, and your .html, for example, is the last value in the array, while the file name is the second to last:
<?
$fileName = explode("." , $file);
?>
To find the length of an array, use the sizeof() PHP attribute.
This will give you the last number able to be used, which will be the extension. We want the one before that (the file name) so we just subtract 1, however, sizeof starts at 1, arrays start at 0, so you really need to subtract 2; 1 to bring it to array count, and another to specify the file name.
<?
$fileName = explode(".",$file);
$fileName = $fileName[sizeof($fileName)-2];
?>
So if $fileName's array had 4 parts inside, (0-3), sizeof($fileName) will equal 4. sizeof($fileName) - 2 equals 2. $fileName[2] should equal the filename.
Good Luck!
If you need further assistance/advice, feel free to contact me.
NOTE: I did not test this script above, well, because I do not have the same script as you, nor the same directories.
 
Back
Top