Jump to content

Help with adding max file size check to upload script?


katlis

Recommended Posts

I'm using a simple upload script to have users upload images.

 

<?
      // (C) 2004 Piotr Szwajkowski
      
      // In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
      // of $_FILES.

   $form = TRUE;
      $upload_path = "c:\\apache\\htdocs\\test\\";

      $fname = 1;

      for ($i=0;$i<10;$i++)
      {
            list($problem, $error) = upload_file ($upload_path, $fname.".gif", $i);
            if (!$problem)
            {
                  $fname++;
            }
            else
                  echo $i.": ".$error."<br>";
      }
      
      echo "<br>";
      echo --$fname." file(s) uploaded";

      
          
      if ($form)
      {
      ?>
          <form enctype="multipart/form-data" action="<?php echo $SELF_PHP; ?>" method="POST">
          <input type="hidden" name="MAX_FILE_SIZE" value="300000">
          0: <input class="file" type="file" name="file[0]"><br>
          1: <input class="file" type="file" name="file[1]"><br>
          2: <input class="file" type="file" name="file[2]"><br>
          3: <input class="file" type="file" name="file[3]"><br>
          4: <input class="file" type="file" name="file[4]"><br>
          5: <input class="file" type="file" name="file[5]"><br>
          6: <input class="file" type="file" name="file[6]"><br>
          7: <input class="file" type="file" name="file[7]"><br>
          8: <input class="file" type="file" name="file[8]"><br>
          9: <input class="file" type="file" name="file[9]"><br>
          <input type="submit" value="Submit Images">
          </form>
      <? 
      } 
     
     
      function upload_file ($upload_path, $fname, $fileid)
      {    
            $problem = TRUE;
            
            switch ($_FILES['file']['error'][$fileid])
            {
                case UPLOAD_ERR_FORM_SIZE:
                     $error = "The uploaded file exceeds the 85kb";
                     break;
                case UPLOAD_ERR_NO_FILE:
                     $error = "No file was uploaded";
                     break;
                default:
                     $error = "";
            }
            
            
            //$fname = $_FILES['file']['name'][$fileid];
            $dest = $upload_path.$fname;
            $tmpfile = $_FILES['file']['tmp_name'][$fileid];
            
            if ($tmpfile)
            {
                $info = getimagesize($tmpfile);
              $ftype = $info['mime'];
            
                if ($ftype != "image/gif" && $ftype != "image/jpeg")
                {
                     $error = "File type not supported";
                     $problem = TRUE;
                }
                else
                {
                     if (file_exists($dest))
                     {
                          $error = "File already exists. (".$dest.")";
                          $problem = TRUE;
                     }                    
                     else
                     {
                          copy ($tmpfile,$dest);                    
                          $problem = FALSE;
                     }
                }
            }
      
            return array($problem, $error);
      }     
?>

 

As you can see, it currently checks for file size via the hidden form field MAX_FILE_SIZE. Not so great.

 

How can I go about adding in php-side file size checking for EACH file, with the maxium size allowed being 1.3mb?

 

I appreciate any help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.