0

I am trying check the file size before uploading to the page. If the size is less than 10 bytes then nothing should display.

Is this the right way to do it? or do i need to change the 10 to something else?

<?php
    $newsFile = "news.txt";
    $Size = ($newsFile);
    $myfile = fopen($newsFile, "r") or die("Unable to open file!");

    if($Size < 10) {
        echo "File is not big enough";
    }
    else {
        while(!feof($myfile)) {
        echo fgets($myfile) . "<br>";
        }
    }

1 Answers1

0

It is not possible to check the size of the file until upload is complete on server side.

It is however possible that you enforce a max file size limit in html which will show a warning but can be bypassed by modifying the frontend html by a clever user. (there is no minimum limit though).

What you can do is, finish the upload and then check the size using

$size = filesize($filename);

If the size is not enough , you can delete the file and return relevant error to the user.

For javascript based implementation, kindly check the answer here

Community
  • 1
  • 1
arkoak
  • 2,437
  • 21
  • 35