Best way to determine if a file is empty (php)?
file_get_contents()
will read the whole file while filesize()
uses stat()
to determine the file size. Use filesize()
, it should consume less disk I/O and much less memory.
Check if specific input file is empty
You can check by using the size
field on the $_FILES
array like so:
if ($_FILES['cover_image']['size'] == 0 && $_FILES['cover_image']['error'] == 0)
{
// cover_image is empty (and not an error)
}
(I also check error
here because it may be 0
if something went wrong. I wouldn't use name
for this check since that can be overridden)
IF file is !empty ELSE
<?
$file = 'config/config2.php';
if(filesize($file)!=0)// NB:an empty 'looking' file could have a file size above 0
{
some code here!
}
else
{
some other code here!
}
?>
how to know if a file is empty or not
The $_FILES always have
[file] => Array
(
[name] => MyFile.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php/php6hst32
[error] => UPLOAD_ERR_OK
[size] => 98174
)
You can use the following to get the size
$_FILES['file']['size']
Check if file-input[] is empty
if ($_FILES['file_input']){
foreach($_FILES['file_input']['name'] as $k=>$v){
if(!empty($_FILES['file_input']['name'][$k])){
if($_FILES['file_input']['size'][$k]>0){
// all ok, can be moved ..
}
}
}
}
How can I use PHP to check if a directory is empty?
It seems that you need scandir
instead of glob, as glob can't see unix hidden files.
<?php
$pid = basename($_GET["prodref"]); //let's sanitize it a bit
$dir = "/assets/$pid/v";
if (is_dir_empty($dir)) {
echo "the folder is empty";
}else{
echo "the folder is NOT empty";
}
function is_dir_empty($dir) {
if (!is_readable($dir)) return null;
return (count(scandir($dir)) == 2);
}
?>
Note that this code is not the summit of efficiency, as it's unnecessary to read all the files only to tell if directory is empty. So, the better version would be
function dir_is_empty($dir) {
$handle = opendir($dir);
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
closedir($handle);
return false;
}
}
closedir($handle);
return true;
}
By the way, do not use words to substitute boolean values. The very purpose of the latter is to tell you if something empty or not. An
a === b
expression already returns Empty
or Non Empty
in terms of programming language, false
or true
respectively - so, you can use the very result in control structures like IF()
without any intermediate values
check if $_FILES is empty or not, file size, file exists
You can get filesize in bytes by:
$_FILES['img']['size']
for checking if it's image file or not or image file format you can check $_FILES['img']['tmp_name']
by getimagesize() like this: php how to use getimagesize() to check image type on upload
for checking if file uploaded or not you can check it with error
. error number 4 means file not uploaded:
if ($_FILES['img']['error']==4){
echo 'Not uploaded';
}
here you can see more information about Error Messages Number
Related Topics
How to Use SQL Server Connection in Laravel
How to Add Commas to Numbers in PHP
Change the PHP Path to Mamps PHP
Using Prepared Statement, How I Return the Id of the Inserted Row
Unit Testing with Items That Need to Send Headers
How to Keep Form Values After Post
Http://Localhost/ Not Working on Windows 7. What's the Problem
How to Send a Http/2 Post Request in PHP
At' Symbol Before Variable Name in PHP: @$_Post
Detect Mobile Devices - and Tablet Devices
Using Curl in PHP with Client Certificate and Private Key in Separate Files