Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP not throwing exception on incorrect file upload size
    primarykey
    data
    text
    <p>I have a simple form with a file upload input. This is the code that handles that form:</p> <pre><code>$error = false; if(isset($_POST["name"], $_POST["desc"], $_POST["aname"], $_FILES["skin"])) { try { // Validate the form if(strlen($_POST["name"]) &lt; 3 || strlen($_POST["name"]) &gt; 24) throw new Exception("Skin name needs to be between 3 and 24 characters."); if(strlen($_POST["desc"]) &lt; 3 || strlen($_POST["desc"]) &gt; 32) throw new Exception("Skin description must be between 3 and 32 characters."); if(strlen($_POST["aname"]) &lt; 2 || strlen($_POST["aname"]) &gt; 16) throw new Exception("Authors name must be between 2 and 16 characters."); // Validate the file upload if($_FILES["skin"]["error"] !== UPLOAD_ERR_OK) throw new Exception("There was a problem uploading the file. Try again later."); if($_FILES["skin"]["size"] &gt; 204800) throw new Exception("File size must be 200KB or less."); } catch (Exception $e) { $error = $e-&gt;getMessage(); } } </code></pre> <p>And then later down in my code I have this:</p> <pre><code>if($error) { echo "&lt;p class=\"error\"&gt;Error: &lt;i&gt;$error&lt;/i&gt;&lt;/p&gt;"; } </code></pre> <p>Which displays an error above the form if an exception was thrown earlier in the code. Everything works fine apart from this section:</p> <pre><code>if($_FILES["skin"]["size"] &gt; 204800) throw new Exception("File size must be 200KB or less."); </code></pre> <p>What I'm trying to do is detect if the file size of the uploaded file is more than 200KB. If it is, I want to throw an exception. But, if I send a 20MB file through the upload form, it doesn't catch it at all, and doesn't throw the exception.</p> <p>Can anyone have a guess at why? I've already tried a few different combinations but for some reason it just doesn't detect it as over 200KB in size.</p> <p>Any help is appreciated, thanks.</p> <p>EDIT: Nevermind, I forgot to change the default php.ini setting that limits POST upload sizes. For anyone that ever has this problem, go into your php.ini file and change this option: <code>post_max_size = 8M</code> to something that suites your needs.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload