-4

I want to ask about PHP Limit upload size validation I have made the code to limit size upload, but thats still get error

My limit size is 500 Kb when I upload file above 500Kb to 2Mb, the validation is working but when my file size above 2Mb, the validation isnt working

here is my first code

$maxsize    = 500000;
if(($_FILES['myfile']['size'] >= $maxsize) || ($_FILES["myfile"]["size"] == 0)) {
                    $error = true;
                    array_push($error_cause, "<li>File size is over limit");
                }

and this is my second code

if ($myfile->getSize() > 500000) {
                    $error = true;
                    array_push($error_cause, "<li>File size is over limit");
                }

To make it clearer, i make a GIF about the problem Here

saintadjie
  • 34
  • 2
  • 9
  • 1
    what is `print_r($myfile->getSize());`? – Rotimi Mar 23 '18 at 08:10
  • What do you mean your validation is not working when the file is over 2MB? Do you get any other error? – yivi Mar 23 '18 at 08:29
  • 1
    Your code should work as it is. And the fact that it works above 500kb should be a **clear** pointer in that direction. If it fails above 2MB, it looks like a webserver / php configuration thing (max upload settings). – yivi Mar 23 '18 at 08:30
  • See https://stackoverflow.com/questions/2184513/change-the-maximum-upload-file-size – Barmar Mar 23 '18 at 08:32
  • 1
    Possible duplicate of [Change the maximum upload file size](https://stackoverflow.com/questions/2184513/change-the-maximum-upload-file-size) – yivi Mar 23 '18 at 08:32
  • I mean, My limit to upload is 500Kb (so I can not upload file up to 500Kb) and my code is working, when I upload file size 501Kb to 2Mb (the validation is working) but when I upload file above 2Mb, the validation was not working and it's processed it but the file was not stored, it should get error message validation because my upload file above 500Kb – saintadjie Mar 23 '18 at 08:33
  • Do you get any other errors when you upload a file over 2MB? – yivi Mar 23 '18 at 08:33
  • no error, but the error message validation should show up because it is above 500Kb – saintadjie Mar 23 '18 at 08:35
  • I'm pretty sure you **are** getting an error, but you are not seeing it. Check your logs, at the very least. – yivi Mar 23 '18 at 08:36
  • @AnandG, no, that's not how it works. Answers should answer the problem, not be _clues_. And if the answers are not useful to the OP, they are completely in their right to vote however they want. – yivi Mar 23 '18 at 08:42
  • 2
    @yivi In that case the OP should explain why, and [downvotes should primarily be used](https://stackoverflow.com/help/privileges/vote-down) for "Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect." – MatsLindh Mar 23 '18 at 08:44
  • @yivi No one has the right to downvote the answer if it relevant but not answering. If you saying has the right to downvote, then no one will write the answer. BTW, if someone is trying to help, OP should learn to appreciate not downvote – Anand G Mar 23 '18 at 08:44
  • No, that's not how [voting works](https://meta.stackoverflow.com/questions/357436/why-isnt-commenting-mandatory-on-downvotes-and-why-are-ideas-suggesting-such-s). – yivi Mar 23 '18 at 08:45
  • Voting is anonymous for a reason. No one is obliged to explain how they vote. Up vote? Someone found your post useful. Down vote? The opposite. **That's the end of it**. But suit yourselves and spend time trying to answer stuff like this. – yivi Mar 23 '18 at 08:47
  • this is my program https://media.giphy.com/media/7JEWXbLtNJVSismkIc/giphy.gif – saintadjie Mar 23 '18 at 08:48
  • @yivi, Dude, you need to learn Basics. If someone scaps your efforts, you have right to ask. Now stop being **** and grow up! – Anand G Mar 23 '18 at 08:49
  • The only one of these answers that will help you is the one from MatsLindh, who's basically copying the duplicate target. You are receiving an error, but you are not seeing because it is lost in the ajax response. **CHECK YOUR LOGS**. And check your PHP configuration. I'm out. – yivi Mar 23 '18 at 08:52
  • the PHP configuration is not wrong I have checked it, just like https://stackoverflow.com/questions/2184513/change-the-maximum-upload-file-size and there's no error in ajax response because it's working above 500k to under 2mb size – saintadjie Mar 23 '18 at 09:00
  • Have you checked your logs? – yivi Mar 23 '18 at 09:10
  • @saintadjie What does `phpinfo(); exit();` at the top of your controller page say for the `upload_max_filesize` and `post_max_size` values? – MatsLindh Mar 23 '18 at 12:24

3 Answers3

2

Arithmetic 101: 5MB ===> 5 * 1024 * 1024 bytes

To keep code clear, I often define units as constants:

<?php
define('KB', 1024);
define('MB', 1048576);
define('GB', 1073741824);
define('TB', 1099511627776);
// Then you can simply do your condition like
ini_set('upload_max_filesize', 5*MB);
if (isset ( $_FILES['uploaded_file'] ) ) {
    $file_size = $_FILES['uploaded_file']['size'];
    $file_type = $_FILES['uploaded_file']['type'];
    if (($file_size > 0.5*MB) && ($file_size < 2*MB)){      
        $message = 'File too large. File must be more than 500 KB and less than 2 MB.'; 
        echo $message; 
    }
  • 1
    Why would the units cause a problem when he's comparing a 2MB file to `500000`? – Barmar Mar 23 '18 at 08:31
  • They wouldn't. This is just some copypasta that is not really relevant to the problem. – yivi Mar 23 '18 at 08:31
  • Just to keep thinks clearly, I defined the units, @saintadjie would like to specify the file size between 500KB and 2000KB, this is the desired code. it's true, I collected it from many places, but I adapted it to respond to the problem. –  Mar 23 '18 at 08:36
  • This doesn't solve the problem at all. The OP can use your code, and will still have the same problem, since it's functionally identical. – yivi Mar 23 '18 at 08:36
  • I can read. No need to try it. Have you read the OPs code? Can you point in which way your code solves the OP problem? – yivi Mar 23 '18 at 08:38
  • Ok. I tried it. **It doesn't solve the OPs problem**. It´s identical. This is too silly. – yivi Mar 23 '18 at 08:45
0

Simple method:

$min =  500; //KB
$max = 2000; //KB

if($_FILES['myfile']['size'] < $min * 1024 || $_FILES['myfile']['size'] > $max * 1024){
 echo 'error';
}
Bernhard
  • 1,852
  • 11
  • 19
  • this is also not working to me For your information My limit is 500Kb (so I can not upload file up to 500Kb) and my code is working, when I upload file size 501Kb to 2Mb (the validation is working) but when I upload file above 2Mb, the validation was not working and it's processed it but the file was not stored, it should get validation because my upload file above 500Kb – saintadjie Mar 23 '18 at 08:31
  • Haven't downvoted anything, but this doesn't solve the problem **at all**. The OP can use your code, and will still have the same problem, since it's functionally identical. – yivi Mar 23 '18 at 08:40
-2

The value of upload_max_filesize in php.ini is 2MB by default. Any file larger than this will be rejected by PHP.

You'll want to change the value in your php.ini file, and make sure to also adjust the post_max_size setting, as this is also considered for uploaded files.

When the uploaded file size is larger than the limit, the $_FILES array will be empty and you won't detect the upload at all (and thus, you won't show any errors).

You can see the actual uploaded file size by looking at the value fo $_SERVER['CONTENT_LENGTH'].

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • 1
    What does your output from `phpinfo();` - without anything else called - show for those settings? Since file uploads are handled before code runs, changing the setting from `ini_set` won't do much. – MatsLindh Mar 23 '18 at 08:37
  • @AnandG I cant devote it, my reputation is below 125, so It's not me – saintadjie Mar 23 '18 at 08:51
  • @saintadjie I figured it out who is the one. I have raised the flag from him/her. Sorry, I took your name. Deleting my comment – Anand G Mar 23 '18 at 08:52