SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can anyone see anything wrong with this code?

    The code below is an upload script, which essentially uploads a file and checks the file type and file size.

    For the most part this works fine, it detects invalid file file extensions, however if the file is over 2mb the script doesnt do what it should, and doesnt output any kind of result or do the upload.

    Is this a memory issue on the server, or is it a issue with coding?

    PHP Code:
    define('MAX_FILE_SIZE'8388608); // 8 MB
    $allowed_types = array('image/gif''image/pjpeg''image/jpeg''image/jpg''image/png''image/bmp');
    $target_path $uploadLocation basename$_FILES['file']['name']);
    $extension substr(strrchr($_FILES['file']['type'], "/"), 1);
        switch (
    $_FILES['file']['error']) {
            case 
    UPLOAD_ERR_NO_FILE:
                  
    $error 'No file selected to upload!';
                  echo 
    $error;
                  break;

            case 
    UPLOAD_ERR_OK:
                if (
    $_FILES['file']['size'] > MAX_FILE_SIZE) {
                    
    $error 'File is too large!';
                    echo 
    $error;
                    break;
                }
                if (!
    in_array($_FILES['file']['type'], $allowed_types)) {
                    
    $error 'That file type is not allowed!';
                    echo 
    $error;
                    break;
                }
                if (
    move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
                    echo 
    "The file: ".  basename$_FILES['file']['name']). " has been uploaded!";  }
                    else {
                    
    $error 'Could not move file, upload failed!';
                    echo 
    $error;
                    break;  }


  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's because of many different settings in PHP.ini and httpd.conf - there is usually a server upload limit of 2mb.

    Do you have access to php.ini and httpd.conf? Note that if you upload the files later to a server, you may have problems.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    default max execution time is 30 seconds. if the upload takes longer than 30 seconds it will fail.

  4. #4
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    It's because of many different settings in PHP.ini and httpd.conf - there is usually a server upload limit of 2mb.

    Do you have access to php.ini and httpd.conf? Note that if you upload the files later to a server, you may have problems.
    Your right the upload limit is 2M in the php config file. Is there a way to return this as a error message instead of displaying nothing?

    Also, when I attempt to upload 70MB file to test (my limit is 8M so im expecting a error from the script) the script craps out, does PHP load the file into memory first even before it transfers it?

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    yup:
    PHP Code:
    define('MAX_FILE_SIZE'2097152); 
    Sets the max size to 2mb.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    yup:
    PHP Code:
    define('MAX_FILE_SIZE'2097152); 
    Sets the max size to 2mb.
    I know that, wondering if I could output that it was a server error as opposed to a script error... Also, that is of no help because if I try to upload a 25mb file, the script works for a while, then just does nothing, I dont even get an error message stating the file is too large.

    Its as if PHP attempts to digest the entire file and ultimately craps out not letting the script do its thing.
    Last edited by tdob; Nov 7, 2007 at 14:37.

  7. #7
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Read this and this

  8. #8
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTTP isn't all that reliable for uploading large files. Even if you get everything configured right I think it's still going to be hit and miss with files more that a few MB

  9. #9
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    HTTP isn't all that reliable for uploading large files. Even if you get everything configured right I think it's still going to be hit and miss with files more that a few MB

    The thing is, I dont want to upload large files. Just .pdf files or image files.

    The limit on the PHP Server is 2mb, which is fine. If a user tries to upload a file up to 7mb they get the appropiate error based on the PHP limit.

    My issue is, if a user tries (for whatever reason) to upload a larger file, nothing happens and the page kind of reloads itself seemingly ignoring all code/error checking. I suppose this is just something you have to deal with PHP/Uploads?

  10. #10
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Shouldn't the class you use deal with files larger than MAX_FILE_SIZE?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  11. #11
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Shouldn't the class you use deal with files larger than MAX_FILE_SIZE?
    Im not 100% sure on what you mean. I have the hidden form field:
    Code:
    <input type="hidden" name="MAX_FILE_SIZE" value="1097152">
    However, im not sure where it comes into play? Is it supposed to output an error?

  12. #12
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tdob View Post
    Im not 100% sure on what you mean. I have the hidden form field:
    Code:
    <input type="hidden" name="MAX_FILE_SIZE" value="1097152">
    However, im not sure where it comes into play? Is it supposed to output an error?
    That should yeld a message on browser side. If the browser does that is another question.

    If file size exceeds this value, a respective error code will be set to $_FILES['userfile']['error'] and upload will be considered as failed.

    If you use this html field attribute, you must handle the error in php too.
    Saul

  13. #13
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No, i mean in the class, it checks for the file size:
    PHP Code:
    if($_FILES['file']['size'] > MAX_FILE_SIZE
    so it should error...
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •