How To Remove File Extention Fron Display?

Started on the old forum in July 2008 · view the original in the archive · old links redirect here

11 replies · 1,114 views · started 18 years ago · 1 min read

7 participants
stretch Developer · 130 posts #1

How do one strip the file extention eg: .jpg .gif .mp3 and so one from displaying when you link to it??

so instead of showing

file1.jpg file2.jpg

it shows

file1 file2

😕

<?

if (!isset($page)) {$page = 0;}
$total = 0;

if(!($dp = opendir("./"))) die ("Cannot open ./");
$file_array = array(); 
while ($file = readdir ($dp))
    {
    if(substr($file,0,1) != '.' and $file != "index.php")
        {
        $file_array[] =  $file;
        }
    }
$file_count = count ($file_array);
sort ($file_array);
?> 

    <p align="left" mode="nowrap">
        
        <?
        
        if ($file_count > 0)
            {
            $first_record = $page * $conf["items_per_page"];
            $last_record = $first_record + $conf["items_per_page"];
            
            while (list($fileIndexValue, $file_name) = each ($file_array))
                {
                
                if (($fileIndexValue >= $first_record) AND ($fileIndexValue < $last_record))
                    {
            
                    echo "<a href=\"$file_name\">$file_name</a> (". round(filesize($file_name)/1024,1) . "kb)
";
                    $total = $total + filesize($file_name);
                    }
                }

= = = = = The more you laugh...the longer you live. Read more jokes at http://fun-files.co.cc

Guest #2

try this CODE<?

if (!isset($page)) {$page = 0;} $total = 0;

if(!($dp = opendir("./"))) die ("Cannot open ./"); $file_array = array(); while ($file = readdir ($dp)) { if(substr($file,0,1) != '.' and $file != "index.php") { $file_array[] = $file; } } $file_count = count ($file_array); sort ($file_array); ?>

    <?
    
    if ($file_count > 0)
        {
        $first_record = $page * $conf["items_per_page"];
        $last_record = $first_record + $conf["items_per_page"];
        
        while (list($fileIndexValue, $file_name) = each ($file_array))
            {
            
            if (($fileIndexValue >= $first_record) AND ($fileIndexValue < $last_record))
                {
                $file_name = $name_file;
                 $name_file= str_replace("jpg", "", $name_file);
                  $name_file= str_replace("gif", "", $name_file);
                   $name_file= str_replace("jpeg", "", $name_file);
                    $name_file= str_replace("mp3", "", $name_file);
        
                echo "<a href=\"$file_name\">$name_file</a> (". round(filesize($file_name)/1024,1) . "kb)

"; $total = $total + filesize($file_name); } }

stretch Developer · 130 posts #3

Now it shows everthing blank 😕

= = = = = The more you laugh...the longer you live. Read more jokes at http://fun-files.co.cc

Attachments: err1.jpg
GumSlone Administrator Principal · 1,690 posts #5

Here is a function which i have used at xchanger.mobi

/////////GET FILE NAME WITHOUT EXTENTION  AND REMOVE PHONE NUMBERS FROM FILE NAME
function strip_ext($name)
 {
     $ext = strrchr($name, '.');
     if($ext !== false)
     {
         $name = substr($name, 0, -strlen($ext));
     }
     $_number = preg_replace('/[^[:digit:]\^]/','',$name);
     if(strlen($_number) > 6) $name = preg_replace('/\d/', '', $name);
     $name = str_replace("____", "_", $name);
     $name = str_replace("___", "_", $name);
     $name = str_replace("__", "_", $name);
     if(strlen($name) < 4) $name = 'File';
     return $name;
}
///////////////////////////////////////

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

modfiles Developer · 200 posts #6

GumSlone wrote: Here is a function which i have used at xchanger.mobi

/////////GET FILE NAME WITHOUT EXTENTION**AND REMOVE PHONE NUMBERS FROM FILE NAME
function strip_ext($name)
 {
**** $ext = strrchr($name, '.');
**** if($ext !== false)
**** {
******** $name = substr($name, 0, -strlen($ext));
**** }
**** $_number = preg_replace('/[^[:digit:]\^]/','',$name);
**** if(strlen($_number) > 6) $name = preg_replace('/\d/', '', $name);
**** $name = str_replace("____", "_", $name);
**** $name = str_replace("___", "_", $name);
**** $name = str_replace("__", "_", $name);
**** if(strlen($name) < 4) $name = 'File';
**** return $name;
}
///////////////////////////////////////

with this function, if a file has a name.name2.jpg the name that will be displayed will be name instead of name.name2

you can just use str_ireplace or str_replace command

///// Strip File Extension
function strip($rep_file) {
$t = array(".gif", ".jpg", ".bmp", ".png" );
$l = array( "", "", "", "" );
$rep_file = str_ireplace($t, $l, $rep_file);
return $rep_file;
}
mcKeny Developer · 130 posts #7

modfiles wrote: with this function, if a file has a name.name2.jpg the name that will be displayed will be name instead of name.name2

you can just use str_ireplace or str_replace command

///// Strip File Extension
function strip($rep_file) {
$t = array(".gif", ".jpg", ".bmp", ".png" );
$l = array( "", "", "", "" );
$rep_file = str_ireplace($t, $l, $rep_file);
return $rep_file;
}

err? that will eliminate the extension alright.....and then the file becomes completely useless. 😛

R.M.C

PHP Adovocate B)

modfiles Developer · 200 posts #8

mcKeny wrote: err? that will eliminate the extension alright.....and then the file becomes completely useless. :P

err... he use the wapbuddy directory list and the line that show the file name is (which is he want to strip the file extension)

echo "<a href=\"$file_name\">$name_file</a> (". round(filesize($file_name)/1024,1) . "kb)

and with the function i posted, it eill be like this

echo "<a href=\"$file_name\">".[b]strip($name_file)[/b]."</a> (". round(filesize($file_name)/1024,1) . "kb)

how does it become useless?

mcKeny Developer · 130 posts #9

modfiles wrote: err... he use the wapbuddy directory list and the line that show the file name is (which is he want to strip the file extension)

echo "<a href=\"$file_name\">$name_file</a> (". round(filesize($file_name)/1024,1) . "kb)

and with the function i posted, it eill be like this

echo "<a href=\"$file_name\">".strip($name_file)."</a> (". round(filesize($file_name)/1024,1) . "kb)

how does it become useless?

😀 my assumption was wrong....i thought it was the file url.....

R.M.C

PHP Adovocate B)

ksg91 Developer · 162 posts #10

another thing can be like this suppose $filename hold's name of file and assuming that file extention is of 3 character.

<?php
$len=strlen($filename);
$filename=substr($filename,0,$len-4);
?>

another thing can be (for all extentions and all length of extention)

<?
$rf=strrev($filename);
$ext=explode(".",$rf);
$ext=strrev($ext[0]);
$filename=str_replace(".$ext","",$filename);
?>

Follow me @ksg91 | My Blog: http://ksg91.com | Nokia Blog: http://NokiaTips.in

neostar Developer · 109 posts #11

Ok here is code:

$txt = strrev($file_name);
$txt = explode(".", $txt);
$txt = strrev($txt[0]);
echo $txt;
neostar Developer · 109 posts #12

Ok here is code:

$txt = strrev($file_name);
$txt = explode(".", $txt);
$txt = strrev($txt[1]);
echo $txt;

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close