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:

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
// 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_name, FTP_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'>";

?>

    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.

      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.

        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:

        
        <script>
        $('#fileToUpload').change(function() {
            var fileSize = this.files[0].size;
        
        alert(fileSize);
        });
        </script>
        
        <input type="file" id="fileToUpload"/>
        
        

          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,

            You need the jQuery library. Below is a working example but I would learn jQuery...which is a Javascript library.

            <!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

              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.

                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!

                  Write a Reply...