PHP - accessing a form-submitted file for validation
<p>I've been doing form validation using $_POST for text entries. Now I have to validate uploaded files through the HTML type='file' forms. I'm having trouble accessing them through $_POST or $_FILES. Is there a different way to do this? I need to get the file so that I can check the MIME type using getimagesize() or mime_content_type()</p>

<p>Here are the relevant bits from my form:</p>

<pre><code>&lt;form action='submission.php' method='POST'&gt;
    &lt;p&gt;Upload photo of professor&lt;/p&gt;
    &lt;input type="file" name="pic" accept="image/*&gt;      
    &lt;p&gt;Upload video of professor&lt;/p&gt;
    &lt;input type="file" name="video" accept="video/*"&gt;
    &lt;p&gt;&lt;input type="submit" onclick="validate()"&gt;&lt;/p&gt;
&lt;/form&gt;
</code></pre>

<p>submission.php is supposed to call on a validation function to check that the MIME type is image, if I can get the file.</p>

<pre><code>function validateimage(&amp;$errors, $field_list, $field_name, $pattern) {
    if (isset($field_list[$field_name])) {
        if (!preg_match($pattern, mime_content_type($field_list[$field_name]))) {
            $errors[$field_name] = 'Please upload an image file';
        }
    } else {
        $errors[$field_name] = 'Required field';
    }
}
</code></pre>

<p>After adding <code>enctype="multipart/form-data"</code> to my field and setting $field_list to $_FILES it almost works. However, it appears <code>isset($_FILES['pic'])</code> is always true, as when I try to submit with no upload, I get this warning:</p>

<p><code>validateimage($errors, $_FILES, 'pic', '/^image/i');</code></p>

<blockquote>
  <p>Warning: mime_content_type(): Empty filename or path in /Applications/XAMPP/xamppfiles/htdocs/cs4ww3/Assignment 3/validate.inc.php on line 36</p>
</blockquote>