Results 1 to 6 of 6

Thread: Limiting upload size

  1. #1
    Join Date
    Aug 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Limiting upload size

    Can anyone tell me how to limit the size of my uploads? This is within the coding (below) but doesn't seem to be working enabling uploads of all sizes. Ideally as well I would like to specify the error like the others, e.g. "the file is too big" so that users know the problem.

    Could it be anything to do with my php settings and if so how can I change them?_regards

    PHP Code:
    <?php
    if (($_FILES["file"]["type"] == "image/gif")
    || (
    $_FILES["file"]["type"] == "image/jpg")
    || (
    $_FILES["file"]["type"] == "image/jpeg")
    || (
    $_FILES["file"]["type"] == "image/pjpeg")
    && (
    $_FILES["file"]["size"] < 200))
      {
      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. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Ok, well first of all, if your uploading images, 200 kb just isn't gonna do it - sorry.
    Also, in your php.ini file you should have something like this:
    Code:
    ;;;;;;;;;;;;;;;;
    ; File Uploads ;
    ;;;;;;;;;;;;;;;;
    
    ; Whether to allow HTTP file uploads.
    file_uploads = On
    
    ; Temporary directory for HTTP uploaded files (will use system default if not
    ; specified).
    upload_tmp_dir = "c:/wamp/tmp"
    
    ; Maximum allowed size for uploaded files.
    upload_max_filesize = 3000m
    The highlighted part may not be what you have, change it to something bigger. Also make sure that the file is also uploading.
    Put a link to your page please if none of those works.
    Jeremy | jfein.net

  3. #3
    Join Date
    Aug 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    200 was just an example.. obviously I will allow more but for testing purposes I set it to that.

    The upload_max_filesize is currently 35M. How can I change these settings? The uploading is working but without restricting the file size. I've put the current code below but I'm worried about putting a test page up as anyone could upload anything to my server.

    PHP Code:
    <?php
    if (($_FILES["file"]["type"] == "image/gif")
    || (
    $_FILES["file"]["type"] == "image/jpg")
    || (
    $_FILES["file"]["type"] == "image/jpeg")
    || (
    $_FILES["file"]["type"] == "image/pjpeg")
    )
        {
        if (
    $_FILES["file"]["error"] > 0)
        {
        echo 
    "Return Code: " $_FILES["file"]["error"] . "<br />";
        }
        else
        {
            if(
    $_FILES["file"]["size"] > 2){
            echo 
    'the file is too big';
            }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";
    }
    ?>

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    What PHP version do you have?
    Jeremy | jfein.net

  5. #5
    Join Date
    Aug 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    5.2.6

  6. #6
    Join Date
    Aug 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Any ideas anyone??

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
  •