Problems with Uploading Script : <p>I have a simple script that can upload files on my server and insert the details into database.</p>

<p>With code below I am getting two errors..</p>

<ol>
<li><p>"Notice: Undefined variable: sExt in" ..
I tried to fix the issue with if empty statement, but without sucess..</p></li>
<li><p>Script import numbers (1,2,3...) into Mysql if the upload filed is empty....
I tried to fix the issue with the code below, but also without success..</p>

<p>"if($_FILES['files']['name']!="")"...</p></li>
</ol>

<p>Any advice? </p>

<p>Thank you..</p>

<p>My code:</p>

<pre><code>&lt;?php

  include_once('db.php');

if (isset($_FILES['files'])) {
    $uploadedFiles = array();
    foreach ($_FILES['files']['tmp_name'] as $key =&gt; $tmp_name) {
        $errors = array();
        $file_name = md5(uniqid("") . time());
        $file_size = $_FILES['files']['size'][$key];
        $file_tmp = $_FILES['files']['tmp_name'][$key];
        $file_type = $_FILES['files']['type'][$key];

        if($file_type == "image/gif"){
            $sExt = ".gif";
        } elseif($file_type == "image/jpeg" || $file_type == "image/pjpeg"){
            $sExt = ".jpg";
        } elseif($file_type == "image/png" || $file_type == "image/x-png"){
            $sExt = ".png";
        }
        if (!in_array($sExt, array('.gif','.jpg','.png'))) {
            $errors[] = "Image types alowed are (.gif, .jpg, .png) only!";
        }


        if ($file_size &gt; 2097152000) {
            $errors[] = 'File size must be less than 2 MB';
        }

        $query = "INSERT into user_pics (`person_id`,`pic_name`,`pic_type`) VALUES('1','$file_name','$sExt')";
        $result = mysqli_query($link,$query);

        $desired_dir = "user_data/";
        if (empty($errors)) {
            if (is_dir($desired_dir) == false) {
                mkdir("$desired_dir", 0700);     
            }
            if (move_uploaded_file($file_tmp, "$desired_dir/" . $file_name . $sExt)) {
                $uploadedFiles[$key] = array($file_name . $sExt, 1);
            } else {
                echo "Files Uploaded !" . $_FILES['files']['name'][$key];
                $uploadedFiles[$key] = array($_FILES['files']['name'][$key], 0);
            }
        } else {
            print_r($errors);
        }
    }

    foreach ($uploadedFiles as $key =&gt; $row) {
        if (!empty($row[1])) {
            $codestr = '$file' . ($key+1) . ' = $row[0];';
            eval ($codestr);
        } else {
            $codestr = '$file' . ($key+1) . ' = NULL;';
            eval ($codestr);
        }
    }

}

?&gt;


&lt;form action="" method="POST" enctype="multipart/form-data"&gt;
    &lt;input type="file" name="files[]" accept="image/*"&gt; &lt;br/&gt;
    &lt;input type="file" name="files[]" accept="image/*"&gt; &lt;br/&gt;&lt;br/&gt;
    &lt;input type="submit"/&gt;
&lt;/form&gt;
</code></pre>
