Image uploader not working - code provided, please help ",)

Discussion in 'PHP' started by le007, Nov 25, 2011.

  1. #1
    Hi all,
    I have the dir at 777 but I'm just getting "Invalid file", any suggestions why? I had this script working before...

    <?php
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    
        if (file_exists("upload/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"], "upload/permanent_name.jpg");
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>
    PHP:
    Thank you :D
     
    le007, Nov 25, 2011 IP
  2. ktmfreelancer

    ktmfreelancer Greenhorn

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000))

    this is your condition.. which is not fullfilled by the file type or file you have uploaded. there might be few things :
    1. either the file you have uploaded is more that the size you have mentioned?
    2. or the file type you have mentioned is not mentioned? you are only allowed to upload pjpeg,jpeg or gif. you cannot upload other files.

    your code has no problem.. code is all fine.
     
    ktmfreelancer, Nov 25, 2011 IP
  3. freelanceinphp

    freelanceinphp Member

    Messages:
    134
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #3
    can you tell me which file you are uploading and you got the error... please attached that file here...
     
    freelanceinphp, Nov 27, 2011 IP
  4. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #4
    image file type is highly ureliable

    use

    $info = getimagesize($_FILES["file"]["tmp_name"]);

    $allowed_types = array(IMG_GIF, IMG_JPEG, IMG_PNG);

    if (in_array($info[2], $allowed_types))
    { .... statments ... }
     
    kmap, Nov 28, 2011 IP
  5. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    le007, Dec 5, 2011 IP