SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast imati0n's Avatar
    Join Date
    Feb 2003
    Location
    Savannah, GA
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image Upload help

    I just need a few lines of code and perhaps you could help me. At the current moment this code only allows me to upload .gif format files. How do i add .jpg/.jpeg, and .png files to the allowed list?

    PHP Code:
    if ($_FILES['imagefile']['type'] == "image/gif"){
        
    copy ($_FILES['imagefile']['tmp_name'], "files/".$_FILES['imagefile']['name'])
        or die (
    "Could not copy"); 
    Thank you in advance

    Regards,
    imati0n.

  2. #2
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it is just jpeg, then you can just use

    PHP Code:
    if ($_FILES['imagefile']['type'] == "image/gif" || "image/jpg") { 
    I think. However if you have lots of them, then I'd be tempted to put them in an array:

    PHP Code:

    $valid
    = array("image/gif","image/jpg","document/pdf"); 
    and check:

    PHP Code:
    if (in_array($_FILES['imagefile']['type'],$valid)) {

    // do it 
    I think those are the right syntaxes...
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  3. #3
    SitePoint Enthusiast imati0n's Avatar
    Join Date
    Feb 2003
    Location
    Savannah, GA
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

  4. #4
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No prob.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  5. #5
    SitePoint Member
    Join Date
    May 2003
    Location
    Newcastle
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by weirdbeardmt
    PHP Code:
    if ($_FILES['imagefile']['type'] == "image/gif" || "image/jpg") { 
    You can't do a comparison like this. The bit after the || will be compared seperately, and as it's non-zero will always return true.

    It would need to be

    PHP Code:
    if ($_FILES['imagefile']['type'] == "image/gif" || $_FILES['imagefile']['type'] == "image/jpg") { 
    But I'd agree the array method is better.

  6. #6
    SitePoint Enthusiast imati0n's Avatar
    Join Date
    Feb 2003
    Location
    Savannah, GA
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks =)

  7. #7
    SitePoint Member
    Join Date
    May 2002
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can I offer the range of allowed types of file to be uploaded right in the form? If I want to display only picture files with extension .gif or .img when the user clicks the button "Browse", what do I need to add on the <INPUT TYPE=FILE ...> line? Thanks.

    <FORM ACTION="<? echo $PHP_SELF ?>" METHOD=POST ENCTYPE="multipart/form-data">
    <INPUT TYPE=HIDDEN NAME="MAX_FILE_SIZE" VALUE="<? echo MAX_FILE_SIZE ?>">
    <INPUT TYPE=FILE NAME="OriginalPath"><P>
    <INPUT TYPE=SUBMIT NAME="Upload">
    </FORM>

  8. #8
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thanhhai
    Can I offer the range of allowed types of file to be uploaded right in the form? If I want to display only picture files with extension .gif or .img when the user clicks the button "Browse", what do I need to add on the <INPUT TYPE=FILE ...> line? Thanks.

    <FORM ACTION="<? echo $PHP_SELF ?>" METHOD=POST ENCTYPE="multipart/form-data">
    <INPUT TYPE=HIDDEN NAME="MAX_FILE_SIZE" VALUE="<? echo MAX_FILE_SIZE ?>">
    <INPUT TYPE=FILE NAME="OriginalPath"><P>
    <INPUT TYPE=SUBMIT NAME="Upload">
    </FORM>
    Unfortunately, to the best of my knowledge, that's not possible. It'd be good if it was!
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky


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
  •