Hello i need help with my PHP script. The Upload function works great, but many files are empty.
Can you help me please to check first IF file is empty or < 1 byte. and Ignore them.
<?php
$vist_page = "post2.php";
include "logger.php";
file_put_contents("outputfile.txt".uniqid(), file_get_contents("php://input"));
?>
Thanks ;)
CodePudding user response:
One way to do that would be to use strlen()
to check the length of the string, which could be done by replacing
file_put_contents("outputfile.txt".uniqid(), file_get_contents("php://input"));
with
$content = file_get_contents("php://input");
if (strlen($content)) {
file_put_contents("outputfile.txt".uniqid(), $content);
}
else {
// Your error response here
}
CodePudding user response:
$filename = 'somefile.txt';
if (filesize($filename) < 1) {
//ignore
} else {
//do stuff here
}
filesize returns the file size in bytes https://www.php.net/manual/en/function.filesize.php
this may be more effecient:
$filename = 'somefile.txt';
if (filesize($filename) > 0) {
//do stuff here
}