PHP Uploader Script Problems

Discussion in 'Web Design and Development' started by Macman1993, Jun 1, 2009.

  1. macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #1
    I have a php uploader script that has some file restrictions on it. It only allows certain kinds of pictures to be added. It also has a restriction that rejects any file over 500mb. I want to be able to upload any kind of file type but at the same time I want to keep the file size limit. Can someone please help me because I don't know php I just got this code off the w3schools page and really don't know how to edit it. If someone could edit the code for me I would really appreciate it.

    Code:
    <?php
    if ($_FILES["file"]["error"] > 0)
      {
      echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
      }
    ?>
    
    <?php
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/mp4")
    || ($_FILES["file"]["type"] == "image/png")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 65540000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>
    
    <?php
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/mp4")
    || ($_FILES["file"]["type"] == "image/png")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 65540000))
      {
      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/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>
    
     
  2. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Change
    PHP:
    if ((($_FILES["file"]["type"] == "image/gif")
    || (
    $_FILES["file"]["type"] == "image/jpeg")
    || (
    $_FILES["file"]["type"] == "image/mp4")
    || (
    $_FILES["file"]["type"] == "image/png")
    || (
    $_FILES["file"]["type"] == "image/pjpeg"))
    && (
    $_FILES["file"]["size"] < 65540000))
      {
    to
    PHP:
    if ($_FILES["file"]["size"] < 65540000)
      {
     
  3. thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #3
    Nope

    Nope didn't work all I got was this.

    Code:
    Parse error: syntax error, unexpected T_STRING in /hermes/bosweb/web112/b1127/glo.clm51193/upload_file.php on line 16
    If you want to test out the uploader I left the bad php script in it so you could see it. clm51193.com/uploader.html
     
  4. macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #4
    What angelwatt gave you is correct. That error generally means you forgot to close a bracket or parentheses. Double check your code, because we cannot see the php from the URL you provided.

    You've typed something wrong.
     
  5. thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #5
    Ok I checked but still not working for me, heres the code I have running on the site now.
    PHP:
    <?php
    if ($_FILES["file"]["error"] > 0)
      {
      echo 
    "Error: " $_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 
    "Stored in: " $_FILES["file"]["tmp_name"];
      }
    ?>

    <?php
    if ($_FILES["file"]["size"] < 65540000
      { 
      if (
    $_FILES["file"]["error"] > 0)
        {
        echo 
    "Error: " $_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 
    "Stored in: " $_FILES["file"]["tmp_name"];
        }
      }
    else
      {
      echo 
    "Invalid file";
      }
    ?>

    <?php
    if ($_FILES["file"]["size"] < 65540000
      {  
      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/" $_FILES["file"]["name"]);
          echo 
    "Stored in: " "upload/" $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo 
    "Invalid file";
      }
    ?>
     
  6. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #6
    You've got a bunch of asterisk in places like below that don't belong. Not sure how they got there, but should be deleted.
    PHP:
    if*($_FILES["file"]["size"]*<*65540000
    **{*  
     
  7. thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #7
    Thats totally weird I have no idea how they got into my post being as they aren't in the code which I copy&pasted into the post. Another computer mystery I guess. Besides those which don't exist in my code anyway does anyone else see what could be wrong? Here is the code the way it really looks on my computer sorry for all the confusion.

    PHP:
    <?php
    if ($_FILES["file"]["error"] > 0)
      {
      echo 
    "Error: " $_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 
    "Stored in: " $_FILES["file"]["tmp_name"];
      }
    ?>

    <?php
    if ($_FILES["file"]["size"] < 65540000
      { 
      if (
    $_FILES["file"]["error"] > 0)
        {
        echo 
    "Error: " $_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 
    "Stored in: " $_FILES["file"]["tmp_name"];
        }
      }
    else
      {
      echo 
    "Invalid file";
      }
    ?>

    <?php
    if ($_FILES["file"]["size"] < 65540000
      {  
      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/" $_FILES["file"]["name"]);
          echo 
    "Stored in: " "upload/" $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo 
    "Invalid file";
      }
    ?>
     
  8. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #8
    Are you still getting error messages? Also, what program are you using to edit the file?
     
  9. thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #9
    Yup still getting the parse error every time I do something. Its totally weird. And I used bbedit to edit the php but it was originally written in text edit.
     
  10. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #10
    It sounds like TextEdit may have left some invisible characters behind. In BBEdit you can turn on invisible characters so you can tell if there are any weird characters. Alternatively I would create a new file and type what you see in the file to make sure no odd characters get introduced.
     
  11. macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #11
    If you're using TextEdit, make sure to go to Format->Make Plain Text.

    I'd recomment downloading TextWrangler (free) to edit PHP files.
     
  12. macrumors newbie

    Joined:
    Jun 2, 2009
    #12
    try copying the code you posted here and pasting it into your php file. It ran without any errors for me.
     
  13. thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #13
    I think your right, I just turned on the show invisible characters setting and it turns out that every space is actually an invisible circle that text edit put in. Thanks for the idea, I should have all of them gone soon and I'll say if that worked.
     
  14. thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #14
    Ahh the drama continues only files that I had originally allowed are working, once I tried a .mp4 video I got this

    Error: 1
    Error: 1
    Return Code: 1

    Heres my code
    PHP:
    <?php
    if ($_FILES["file"]["error"] > 0)
      {
      echo 
    "Error: " $_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 
    "Stored in: " $_FILES["file"]["tmp_name"];
      }
    ?>

    <?php
    if ($_FILES["file"]["size"] < 65540000
      { 
      if (
    $_FILES["file"]["error"] > 0)
        {
        echo 
    "Error: " $_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 
    "Stored in: " $_FILES["file"]["tmp_name"];
        }
      }
    else
      {
      echo 
    "Invalid file";
      }
    ?>

    <?php
    if ($_FILES["file"]["size"] < 65540000
      {  
      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/" $_FILES["file"]["name"]);
          echo 
    "Stored in: " "upload/" $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo 
    "Invalid file";
      }
    ?>
    I really appreciate the time you guys are giving this. Hopefully you can fix the script so only files that are more then 500mb get denied.
     
  15. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #15
    From http://us3.php.net/manual/en/features.file-upload.errors.php
    I haven't really dealt with file uploading. Have you tried small files or large ones? Also, have you tried various types of files e.g., images, text, etc.?
     
  16. thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #16
    Everything works that is under 1mb I have raised the file limit to a number so ridiculously huge but it still wont work. This is getting really frustrating.
     
  17. thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #17
    I have finally found the part of the php.ini that I had to edit thank you guys so much for the help I now have exactly what I wanted in my uploader.
     

Share This Page