How To Remove File Extention Fron Display?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How To Remove File Extention Fron Display?

    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



    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) != &#39;.&#39; 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

    #2
    try this <div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'><?

    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) != &#39;.&#39; 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))
    {
    $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);
    }
    }











    </div>

    Comment


      #3
      [attachment=1699:err1.jpg]

      Now it shows everthing blank
      Attached Files
      = = = = =
      The more you laugh...the longer you live.
      Read more jokes at
      http://fun-files.co.cc

      Comment


        #4
        [attachment=1699:err1.jpg]

        Now it shows everthing blank [/b]
        you cant strip file extension but can hide them so that people wont able to find them
        but
        let me know 1st why cant you fix easy thing?

        Comment


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

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

          Comment


            #6
            Originally posted by GumSlone View Post
            Here is a function which i have used at xchanger.mobi

            Code:
            /////////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
            Code:
            ///// 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;
            }

            Comment


              #7
              Originally posted by modfiles View Post
              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
              Code:
              ///// 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. :P

              R.M.C
              ----------
              PHP Adovocate B)

              Comment


                #8
                Originally posted by mcKeny View Post
                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)
                Code:
                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
                Code:
                echo "<a href=\"$file_name\">".[b]strip($name_file)[/b]."</a> (". round(filesize($file_name)/1024,1) . "kb)
                how does it become useless?
                Last edited by modfiles; 09.06.09, 02:25.

                Comment


                  #9
                  Originally posted by modfiles View Post
                  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)
                  Code:
                  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
                  Code:
                  echo "<a href=\"$file_name\">".[b]strip($name_file)[/b]."</a> (". round(filesize($file_name)/1024,1) . "kb)
                  how does it become useless?
                  :D my assumption was wrong....i thought it was the file url.....

                  R.M.C
                  ----------
                  PHP Adovocate B)

                  Comment


                    #10
                    another thing can be like this
                    suppose $filename hold's name of file and assuming that file extention is of 3 character.
                    Code:
                    <?php
                    $len=strlen($filename);
                    $filename=substr($filename,0,$len-4);
                    ?>
                    another thing can be (for all extentions and all length of extention)

                    Code:
                    <?
                    $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

                    Comment


                      #11
                      Ok here is code:
                      PHP Code:
                      $txt strrev($file_name);
                      $txt explode("."$txt);
                      $txt strrev($txt[0]);
                      echo 
                      $txt

                      Comment


                        #12
                        Ok here is code:
                        PHP Code:
                        $txt strrev($file_name);
                        $txt explode("."$txt);
                        $txt strrev($txt[1]);
                        echo 
                        $txt

                        Comment

                        Working...
                        X