File Upload Permission Error

Discussion in 'PHP' started by venardv, Jul 20, 2009.

  1. #1
    My code is
    
    <?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"],
          "/home/username/public_html/upload/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>
    
    PHP:
    and then i get an error saying:


    Warning: move_uploaded_file(upload/banner.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/username/public_html/upload/index.php on line 42

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phppMwQ2k' to 'upload/banner.jpg' in /home/username/public_html/upload/index.php on line 42

    can anyone solve this problem? it's really annoying :(
     
    venardv, Jul 20, 2009 IP
  2. iam.xavier

    iam.xavier Well-Known Member

    Messages:
    521
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    125
    #2
    Check upload directory's write permission in "/home/username/public_html/upload/"

    Thanks
    -Xak
     
    iam.xavier, Jul 20, 2009 IP
  3. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It could be a problem with permissions, chmod the upload folder to 757,

    Or it's often a problem with the path, it might not exist, I see you have "username" in there, shouldn't it be your username? or is your username username :)
     
    wd_2k6, Jul 20, 2009 IP
  4. tech36.com

    tech36.com Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Good point.

    Try CHMOD 755
     
    tech36.com, Jul 20, 2009 IP
  5. Loget

    Loget Peon

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Shouldn't you move the file first and then echo were its been uploaded too?
     
    Loget, Jul 20, 2009 IP
  6. venardv

    venardv Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    okay thanks everyone for your help. i had to chmod it to 757. now it's working fine!
     
    venardv, Jul 20, 2009 IP