SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP File Upload Extension

    Hi all,

    I am currently writing up an upload script, to be used by me and friends to work collabratively, and as a technical exercise for me. I want all types of files to be able to be uploaded, but, am getting sick and tired of how PHP won't tell me the blasted file extension by default!

    So, I thought about exploding out everything after a ., and returning the last block of the explode. However, how can I do this? There may be more than one . in the file name, and so that won't work, or is there a better way I have overlooked?

    Thanks,

    Adam

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Texas
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look in the $_FILES variable on upload.

    http://www.tizag.com/phpT/fileupload.php

  3. #3
    SitePoint Member
    Join Date
    May 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you copy and paste the php?

    something simple like this should work

    // where the file is being uploaded to
    $target_path = "upload/";

    /* Adding the filename to the target path.
    ends up showing "upload/name.ext" */
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

    Edit:

    the code above is rather similar to the link provided above.. ha
    Last edited by adamstrawson; May 20, 2009 at 12:06. Reason: revised

  4. #4
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I guess I should have explained a little more. The reason I can't do that is I am manipulating the file name myself, to something a lot more reasonble than whatever clients have (spaces and other nasties!). So, both methods don't work! With images I've done regular expressions on each of the pieces of mimetypes that could be available (which didn't take me too long), as there are only three. However, surely PHP must be able to work out an extension, or, I must be able to use my explode idea!

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Texas
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If that doesn't work for you, I guess you could explode on the '.' and use the last value in the array as the file extension.

  6. #6
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But, how can I determine which is the last value in the array? It could be variable?

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Texas
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about array_reverse() and take the first value $arr[0]?

    http://www.php.net/array_reverse

  8. #8
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Texas
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure exactly what you're trying to do here, but I think we've tunnel visioned in on exploding the filename, or renaming it at all for that matter.

    What's wrong with using the filename that the user uploads? I typically take their name, do a str_replace() on spaces (because I don't like spaces in file names) and then urlencode() the rest.

    If you're worried about duplicate file names, you can append a uniqid() or a date() to the filename.

  9. #9
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I've now learnt about a new function I never knew existed (array_reverse), and agree with you. This is pointless, I could have just put my time before the filename. Thanks all for the help.

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Keeton View Post
    how about array_reverse() and take the first value $arr[0]?

    http://www.php.net/array_reverse
    Just for reference, I think end() would be better suited.

    PHP Code:
    <?php
    $aArray 
    range('a''z');
    echo 
    end($aArray); #z
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #11
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Texas
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Indeed! Good call.


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
  •