File size trap not working in ftp upload script
Results 1 to 8 of 8

Thread: File size trap not working in ftp upload script

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    18

    File size trap not working in ftp upload script

    Iím hoping someone can help me with my problem. First, let me apologize for being wordy. This problem has me stumped and I want to explain it fully.

    Iíve written a PHP upload routine using FTP commands to allow users to upload up to 5 files at a time through an html form, and it works fine for small files. I tried all the fixes suggested in other posts (php.ini, .htaccess, set_ini, etc.) and nothing worked. I finally contacted my server people and it turns out that thereís a server limit on communication time which effectively limits the file size to under about 2-2.5M.

    Since 95% of the files uploaded will be below this, itís not really a problem for me. However, there is that other 5%, so I thought Iíd just insert some code that checked the files size and notify the user that the file was too large and the upload was skipped.

    The code looks like this:


    PHP Code:
    if(filesize($tmp_name)/1024 2048) {
    echo 
    "File: " $tmp_name " exceeds filesize limit.  Upload failed.<p>";
    }
    else {
    Ö rest of code Ö 
    The problem is that when it hits a large file, the code isnít executed. If the user specifies 3 files, for example (small, large, small), the first and third are uploaded and the second times out. The notification to the user is that the first and third uploaded successfully, but nothing about the second. Itís like it never sees the code.

    However, if I change the line to skip file sizes less than 2M (change the > to a <) it bypasses the first and third and times out on the second and tells the user that the first and third were skipped, but again, nothing about the second, so I donít think itís a coding problem.

    Can someone give me some insight as to whatís going on here and, hopefully, how to fix it?

    Thanks, hereís the complete code:

    PHP Code:
    <?php
    // set up basic connection
    $ftp_server "web1203.ixwebhosting.com";
    $ftp_user_name $_POST[user];
    $ftp_user_pass $_POST[passwd];
    $conn_id ftp_connect($ftp_server); 

    // login with username and password
    $login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass); 

    // check connection
    if ((!$conn_id) || (!$login_result)) {
        die(
    "FTP connection has failed !");
    }

    foreach (
    $_FILES["graphicfile"]["error"] as $key => $error) {
      if (
    $error == UPLOAD_ERR_OK) {
        
    $path"".$HTTP_POST_FILES['graphicfile']['name'][$key];
        
    $tmp_name $_FILES["graphicfile"]["tmp_name"][$key];
        
    $name $_FILES["graphicfile"]["name"][$key];
        if(
    filesize($tmp_name)/1024 2370) {
          echo 
    "File: " $tmp_name " exceeds filesize limit.  Upload failed.<p>";
        }
        else {
          if (
    ftp_put($conn_id$name$tmp_nameFTP_BINARY)) {
            echo 
    "File: " $name " uploaded successfully.<p>";
          }
          else {
            echo 
    "File: " $tmp_name " upload failed.<p>";
          }
        }
      }
    }

    // close the connection
    ftp_close($conn_id);

    echo 
    "<meta http-equiv='refresh' content='5;url=index.html'>";

    ?>

  2. #2
    Pna lbh ernq guvfŅ
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,420
    One thing I notice is that you never do anything if the file upload status is not UPLOAD_ERR_OK. Perhaps you should add an 'else' branch that tells you which error occurred in such cases, since it sounds like you're saying your filesize() code isn't even being executed for a given upload.

  3. #3
    Junior Member
    Join Date
    May 2008
    Posts
    18
    Yup, that's what it is. It's always the obvious you overlook, isn't it! That solves the messaging problem, but leads me to another question. I thought $FILES only processed the array passed from the HTML code. But it looks like it's actually trying to upload the file. There is a long delay between the if ($error == UPLOAD_ERR_OK) statement and the (now new) else statements. Is there a way to check the size of the source file without PHP actually trying to upload it?

    Thanks.

  4. #4
    Senior Member
    Join Date
    Oct 2005
    Location
    Chicago, IL
    Posts
    428
    PHP is server side so you're not going to be able check before upload. However, w/ HTML5 and jQuery you can check the file size beforehand. That being said it will not work on all browsers.

    example w/ jQuery:
    Code:
    <script>
    $('#fileToUpload').change(function() {
        var fileSize = this.files[0].size;
    
        alert(fileSize);
    });
    </script>
    
    <input type="file" id="fileToUpload"/>
    Bob Lob Law

  5. #5
    Junior Member
    Join Date
    May 2008
    Posts
    18
    Hey jeepin81. I tried your code but couldn't get it to work in any of my browsers. I'm not familiar with jquery so I'm probably doing it wrong. If you could let me know what else I need to get it to run I'd really appreciate it.

    Thanks,

  6. #6
    Senior Member
    Join Date
    Oct 2005
    Location
    Chicago, IL
    Posts
    428
    You need the jQuery library. Below is a working example but I would learn jQuery...which is a Javascript library.

    PHP Code:
    <!DOCTYPE html>
    <
    html>
    <
    head>
    <
    title>Get File Size Client Side</title>
    <
    script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script type="text/javascript">
    $(window).load(function(){
        $('#fileToUpload').change(function() {
            var fileSize = this.files[0].size;
            alert('File Size: ' + fileSize);
        });
    });
    </script>
    </head>
    <body>
    <input type="file" id="fileToUpload"/>
    </body>
    </html> 
    HTH
    Bob Lob Law

  7. #7
    Junior Member
    Join Date
    May 2008
    Posts
    18
    Many thanks, jeepin81. Checked out the code. Works fine in Firefox and Opera. Get a filesize undefined in Safari and it doesn't work in IE (I know, I hate IE too, but my users like it). It looks like there is no cross browser solution to my problem. Guess I'll just have to warn them what the file size limit is and hope they abide by it.

    If you have any other ideas, I'd love to hear them.

    Again, many thanks.

  8. #8
    Senior Member
    Join Date
    Oct 2005
    Location
    Chicago, IL
    Posts
    428
    I believe there is a way to check IE w/ javascript and ActiveX but I'm not sure off the top of my head... Ask Dr. Google he/she/it is pretty smart.

    An ideal situation is for developers working world wide to stop catering to the likes of people using IE and require them to upgrade but it is a sad, sad world we live in. Go Cubs!
    Bob Lob Law

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •