Exceeding the maximum file size does not show an error

I wrote a code that should check if the file size is more than 8.5 MB or not, and if it should make mistakes as well as prevent the message from being sent to the DB. The code prevents the message from being sent to the database, but does not show any errors indicating that the file is larger than. (PS: Checking for Unknown File Format works.) Here's the code I wrote:

   //$session id
define ("MAX_SIZE","9000"); 
function getExtension($str)
{
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}


$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") 
{

    $uploaddir = "uploads/"; //a directory inside
    foreach ($_FILES['photos']['name'] as $name => $value)
    {

        $filename = stripslashes($_FILES['photos']['name'][$name]);
        $size=filesize($_FILES['photos']['tmp_name'][$name]);
        //get the extension of the file in a lower case format
        $ext = getExtension($filename);
        $ext = strtolower($ext);

        if(in_array($ext,$valid_formats))
         {
           if ($size < (MAX_SIZE*1024))
           {
             $image_name=time().$filename;
             echo "<img src='".$uploaddir.$image_name."' class='imgList'>";
             $newname=$uploaddir.$image_name;

             if (move_uploaded_file($_FILES['photos']['tmp_name'][$name], $newname)) 
             {
               $time=time();
               mysql_query("INSERT INTO user_uploads(image_name,user_id_fk,created) VALUES('$image_name','$session_id','$time')");




           }
           else
           {
             echo '<p style="color: Red;">You have exceeded the size limit! so moving unsuccessful! </p>';
            }

           }
           else
           {
             echo '<p style="color: Red;">You have exceeded the size limit!</p>';

           }

          }
          else
         { 
            echo '<p style="color: Red;">Unknown extension!</p>';

         }

     }
}

      

+3


source to share


1 answer


I changed the code a bit.

You don't need the getExtension function to be that complicated.

I changed foreach to loop through files not via file attributes.

Finally, you need to check if the directory exists before moving the file. If not, you must create one.



if(!is_dir($uploaddir)) {
    mkdir($uploaddir);
}

      

See if this works and check the differences:

<?php
  //$session id
define ("MAX_SIZE","9000"); 
function getExtension($str)
{
         $ext = explode("/",$str);
         return $ext[1];
}


$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") 
{

    $uploaddir = "uploads/"; //a directory inside
    foreach ($_FILES as $FILE)
    {

        $filename = stripslashes($FILE['name']);
        $size=$FILE['size'];
        //get the extension of the file in a lower case format
        $ext = getExtension($FILE['type']);
        $ext = strtolower($ext);

        if(in_array($ext,$valid_formats)){
           if ($size < (MAX_SIZE*1024)){
             $image_name=time().$filename;
             echo "<img src='".$uploaddir.$image_name."' class='imgList'>";
             $newname=$uploaddir.$image_name;

             //Before you upload the file to the directory, check if it exists like this
             if(!is_dir($uploaddir)) {
               mkdir($uploaddir);
             }

             if (move_uploaded_file($FILE['name'], $newname)){
               $time=time();
               mysql_query("INSERT INTO user_uploads(image_name,user_id_fk,created) VALUES('$image_name','$session_id','$time')");
             }else{
             echo '<p style="color: Red;">You have exceeded the size limit! so moving unsuccessful! </p>';
             }
           }else{
             echo '<p style="color: Red;">You have exceeded the size limit!</p>';
           }
          }else{ 
            echo '<p style="color: Red;">Unknown extension!</p>';

         }

     }
}

      

It finally worked on my computer, so let's assume it will work for you. Hope i helped

+2


source







All Articles