2

I have a form where I want to allow files equal or less than 50 MB. but when I upload a file of 1.60 MB it says max file size please upload less. any suggestions why?

the file size 1.60 MB and If I print file array it shows size = 1686220

// Attempt 1

if (!filesize($file_path) < 50000) {
  // max file size limit reached
}

// Attempt 2

if (!$file['size'] < 50000) {
  // max file size limit reached
}
  • Depending on the web server, you might need to configure that separately as well. Ex: nginx has the `client_max_body_size` setting. – M. Eriksson Mar 22 '20 at 11:25
  • I don't want to edit the php.ini file. I want to check file size on form submission. –  Mar 22 '20 at 11:27

1 Answers1

1

you can do it using jquery before submit as well for sample:

    <form action="upload" enctype="multipart/form-data" method="post">

        Upload image:
        <input id="image-file" type="file" name="file" />
        <input type="submit" value="Upload" />

        <script type="text/javascript">
            $('#image-file').bind('change', function() {
                alert('This file size is: ' + this.files[0].size/1024/1024 + "MB");
            });
        </script>

    </form>

using PHP:

if (isset ( $_FILES['file'] ) ) {

    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];

    if (($file_size > 50*1024*1024)){      
        $message = 'File too large. File must be less than 50 MB.'; 
        echo '<script type="text/javascript">alert("'.$message.'");</script>'; 
    }
}
Ronak Dhoot
  • 2,322
  • 1
  • 12
  • 19
  • It works, thank you. can you please explain what you did on php one and what I was doing wrong? thanks again. –  Mar 22 '20 at 11:33
  • @KevinJ please check your less than symbol it should be greater than... :) – Ronak Dhoot Mar 22 '20 at 11:46