SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to get a file name without the extension

    I'm coding an image upload script and I want to replace the file name with a 8 digit random letter/number combination. Is there any way to get the name of a file without the extension? The code I've been using at the minute is

    Code:
    $name = basename($file_name) . random(8);
    But this adds the random letter/numbers after the extension and so doesn't work.

    Any help is appreciated.
    Thanks.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    $pieces = explode(".", basename($file_name));
    $name = $pieces[0] . random(8);

  3. #3
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code doesn't add the .gif or .jpg back on.

  4. #4
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    function doRenameFile($szFilename$szNewName)
    {
        
    $szExtension array_pop(explode('.'$szFilename));
        
        return 
    $szNewName '.' $szExtension;
    }

    echo 
    doRenameFile('test.jpg'345435);

    ?>
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  5. #5
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $name preg_replace('/(?=\.[^.]*$)/'random(8), $file_name); 

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by dazisonfire View Post
    Your code doesn't add the .gif or .jpg back on.
    This line

    PHP Code:
    $pieces explode("."basename($file_name)); 
    splits the file name in two pieces:

    $pieces[0] contains the file name without extension.
    $pieces[1] contains the extension.

    You can do whatever you want with them.

    Quote Originally Posted by trib4lmaniac View Post
    PHP Code:
    $name preg_replace('/(?=\.[^.]*$)/'random(8), $file_name); 
    Looks cool trib4l
    What exactly does it do?

  7. #7
    SitePoint Zealot joazito's Avatar
    Join Date
    Mar 2005
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    guido2004, what if the filename contains dots? trib4lmaniac's solution seems to cover that.

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Not to burst anyones bubble but why not use pathinfo?

    http://www.php.net/manual/en/function.pathinfo.php
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I worked on an uploader some time ago.

    From PHP 5.2 onwards use fileinfo() to get the filename and the extension as seperate parts of an array - I understand it works like this:
    PHP Code:
    //where $orig is the filename only, like this: some.thing.jpg

    $file_info pathinfo$orig ) ;
    $ext $file_info['extension'];
    $file $file_info['filename'] ; 
    Prior to 5.2 I managed a workround like this:

    PHP Code:
    $file_info pathinfo$orig ) ;
    $ext $file_info['extension'];

    if(
    $ext ){

     
    $type_offset strposstr_rev$orig ) , '.');
    //reverse the string and find the position of the last dot
     
    $filenamesubstr$orig 0, -( type_offset +));
    //returns the filename without the extension and the dot leaving you the name "some.thing"


    It seemed to work for me.

  10. #10
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any upload script should check that the file is an allowed file type anyway. However I wouldn't rely on the $_FILES array to report the valid type for the file. It is also important as it was mentioned that you don't fall victim to the "nasty.gif.php" attack. Some of the examples posted are fine, I use..

    PHP Code:
    $fileArr explode('.',$filename);
    $ext $fileArr[(count($fileArr) - 1)];
    // so for your purposes
    $newname $randomname.'.'.$ext 

  11. #11
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Quote Originally Posted by trib4lmaniac
    PHP Code:
    $name preg_replace('/(?=\.[^.]*$)/'random(8), $file_name); 
    Looks cool trib4l
    What exactly does it do?
    It uses a regular expression to insert whatever "random(8)" returns before the final period in a string. Of course, if there are no periods, no insertions will be performed.

    This version handles the aforementioned edge case:
    PHP Code:
    $name preg_replace('/([^.]*)((\.[^.]*)?$)/''${1}' random(8) . '$2'$file_name1);

    // test => test12345678
    // test.html => test12345678.html
    // test.class.php => test.class12345678.php 
    I tried to write my original version using only lookarounds (no back references in the replacement), which is why I neglected to include this functionality previously.

  12. #12
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by joazito View Post
    guido2004, what if the filename contains dots? trib4lmaniac's solution seems to cover that.
    If anyone were really against using regex (I am all for regex, but as an example), a way to handle it would be:
    Code:
    $filename = 'example.with.dots.php';
    list($extension, $filename) = array_map('strrev', explode('.', strrev($filename), 2));
    
    print $filename; // => example.with.dots
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  13. #13
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    As always with php multiple ways to do something.

    PHP Code:
    <?php

    function splitFilenameA ($f) {

        
    $f explode('.'$f);
        
    $e array_pop($f);
        return array(
    join('.'$f), $e);

    }

    var_dump(splitFilenameA('example.with.dots.php'));

    function 
    splitFilenameB ($f) {

        list(
    $e$f) = array_map('strrev'explode('.'strrev($f), 2));
        return array(
    $f$e);

    }

    var_dump(splitFilenameB('example.with.dots.php'));
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  14. #14
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    It isn't my thread, but I learned quite a few things from you guys, so thx all


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •