$filename without extension

Discussion in 'PHP' started by BABUPATHAN, Jul 24, 2008.

  1. #1
    i have a script which us to read jpg images in a folder where this script file exist and displays in a gallery on browser
    it read file name by $filename
    i want to change little that it read file names but not its extension

    please tell me how can i do this
     
    BABUPATHAN, Jul 24, 2008 IP
  2. CristianR

    CristianR Peon

    Messages:
    704
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try using the explode method ...

    $newfilename = array();
    $newfilename = explode(".",$filename);
    $filename = $newfilename[0];

    try this and tell me how it works ..
     
    CristianR, Jul 24, 2008 IP
  3. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You should use this method instead:
    $filename = '/www/path/to/picture/picture.jpg';
    
    $fileParts = pathInfo($fileName);
    PHP:
    this method will give you access to specifics using a well defined array instead of an arbitrarily indexed array:
    
    echo $fileParts['dirname'];     //  /www/path/to/picture
    echo $fileParts['basename'];    //  picture.jpg
    echo $fileParts['filename'];    //  picture
    echo $fileParts['extension'];   //  jpg
    PHP:
    The reason you'd want to do it this way is to provide fault tolerance for random naming conventions.

    Lets say your file name is = 'Auth.class.php'
    If you explode on '.', the first element [0] will give you 'Auth', which is not
    the correct filename.
     
    ahowell, Jul 24, 2008 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    $file = 'image1.3242542.abcdefg.jpg';
    
    //reading the image file name except the dot and last three letter
    echo substr($file,0,-4); 
    PHP:
    :)
     
    php-lover, Jul 24, 2008 IP
  5. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #5
    $file = 'sweetPic.jpeg';

    ;)
     
    ToddMicheau, Jul 24, 2008 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Orrrr....:
    
    echo basename($filename, '.jpg');
    
    PHP:
     
    nico_swd, Jul 24, 2008 IP
  7. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #7

    He said jpg :)
     
    php-lover, Jul 24, 2008 IP
  8. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Lol, well in a real life situation he would probably accept every kind of image, even .jpeg's . . .

    
    pathInfo();
    // is the best method to go with.
    
    PHP:
     
    ToddMicheau, Jul 24, 2008 IP
  9. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #9
    Okay here's your code. work in all file extension.:)


    
    
    $file = 'image1.3242542.abcdefg.jpeg';
    echo substr($file,0,-(strlen(strrchr($file,'.'))));
    
     
    PHP:
     
    php-lover, Jul 24, 2008 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    But why calling 3 functions when all you need is one? pathinfo() and basename() will work just fine...
     
    nico_swd, Jul 24, 2008 IP
  11. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #11
    Agreed- though either way does work, I'm sure we could even come up with a 4th way of doing this x]
     
    ToddMicheau, Jul 24, 2008 IP
  12. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #12
    you have to specify jpg when you using basename.
     
    php-lover, Jul 24, 2008 IP
  13. tgkprog

    tgkprog Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    4th way i have my own last char of did not know about the php 1 ... :) but i like strrchr over pathinfo() cause path info does a lot internally ....
     
    tgkprog, Jul 24, 2008 IP
  14. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #14
    Agreed, pathinfo() was design to get file path and it's do more work internally to find the file path.
     
    php-lover, Jul 25, 2008 IP