SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php explode muliple file extensions help

    Hi im trying to explode multiple file extension, i tried the following and get no joy

    TRY 1

    PHP Code:
    $allfiles = array(".jpg"".png"".gif");
    $exten explode('$allfiles',$file); 
    TRY 2

    PHP Code:
    $exten explode('.jpg','.gif','.png',$file); 

    Can anyone point me in the right direction please, ive pretty much hit a brick wall.

    Thanks in advance for any help!

  2. #2
    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 am a bit confused - are you trying to detect if $file, a filename, is one of the type jpg, gif, or png?

    If so, does filename include the directory path too?
    Where is filename coming from, user input or are you reading all files in a directory?

    If not, can you explain with an example of success and failure?

  3. #3
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basicly its a static php gallery

    PHP Code:
    <?php echo $file?>
    Would result

    PHP Code:
    filename.jpg 
    However if i do this

    PHP Code:
    $exten explode('.jpg',$file); 
    and echo this

    PHP Code:
    <?php echo $exten[0]; ?>
    It will result

    PHP Code:
    filename 
    But as you can see that is only 1 file type, i need to define more file extensions in this code

    PHP Code:
    $exten explode('.jpg',$file); 

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Cardiff
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if your trying to find the extension of the file use

    Code:
    $ext = strrchr($file,'.');
    to do it with explode you would do

    Code:
    $ext = explode(".",$file);
    or if you want the file name use
    Code:
    $file = substr($file, 0, strrpos($file, "."));
    Thanks
    Paul
    1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21

  5. #5
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why not seperate your files with a colon, then explode using the colon.

    PHP Code:
    $file 'one.jpg:two.png:three.gif:four.php';
    $exp explode (":" $file); 
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  6. #6
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Para View Post
    But as you can see that is only 1 file type, i need to define more file extensions in this code

    PHP Code:
    $exten explode('.jpg',$file); 
    use pathinfo -
    PHP Code:
    $segments pathinfo($file);
    if (
    in_array($segments['extension'],$allfiles)) {
        
    printf("The filename is %s<br>\nThe extension is %s<br>\n",basename($file,$segments['extension']),$segments['extension']);


  7. #7
    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)
    Right, so you have a file name and what you want to do is to check if that file extension is one of the ones in an array of allowed file extensions?

    (in this case allowed image files)

    Am I right? Putting together some of the ideas from your friends above:

    PHP Code:
    //dont need the dot
    $images = array('jpg''gif');

     
    /**
     * Uncomment the line below to test your array is correct !.
     **/

    //print_r($images);

     /**
     * Uncomment one of these below to test the whole thing.
     **/

    //$filename = "test.doc";  //bad
    $filename "test.jpg";  //good

    $typesplit("\."$filename);  // using split instead of explode

    /* heres another sanity test - uncomment it to see the output */

    //echo $type[1];

    /* then heres the jobby */

    if(in_array($type[1], $images )){
    echo 
    "thats OK";
    //go on and do something useful
    }else{
    echo 
    "BAD file, tsk";
    //send away

    I left a load of comments in there to help you to get your head round what is going on - it looks like you need to understand step by step what is happening - and it doesnt all have to be in your head - echo it and print_r it onto the page ...

    Now in PHP there is more than one way to skin a cat, and others may well, ( quite rightly too ) post better solutions, yknow, shorter, quicker, less verbose with more checks and features etc.

    But I hope this helps you get a grip on what is going on with arrays. Its fundamental to any programming, really - be prepared to take a few days off and learn about them - there are about 80 array functions in PHP - I mean really have a play with some of the more fundamental ones, time well spent.

    There are some good PHP array sites/tutorials appearing - Goog em.

    The obvious problems with the code I posted are:
    If the file name contains a dot it'll fail "great.big.jpg"
    If someone uploads a .php file but calls it a jpg you wont pick it up
    (read up on mime-type to find a better way of testing for this)
    Its not a function yet - thats some thing you should look at making yourself

    //function checkType($array_of_types, $filename){}

    HTH
    Off Topic:

    Drat, didnt read catweasels far better solution, still, will leave this up as it might help the OP, and I invested many, many minutes in writing it ... still warms my fingers up for the day ... bring it on!

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    strtok($filename'.'); 
    returns everything before the first dot.
    PHP Code:
    echo strtok('foo.jpg''.'); // foo
    echo strtok('barbaz.png''.'); // barbaz 
    hope this helps.


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
  •