filesize() function
<?php $filename = 'example.txt'; $filesize = filesize($filename); if ($filesize !== false) { echo "The size of the file is: $filesize bytes"; } else { echo "Failed to get the file size."; } ?>
Free PHP Code Examples
filesize() function
<?php $filename = 'example.txt'; $filesize = filesize($filename); if ($filesize !== false) { echo "The size of the file is: $filesize bytes"; } else { echo "Failed to get the file size."; } ?>
finfo_file() function
<?php $filename = 'example.jpg'; $file_info = finfo_open(FILEINFO_MIME_TYPE); $mime_type = finfo_file($file_info, $filename); finfo_close($file_info); echo "The file type is: $mime_type"; ?>
<?php $filename = 'example.txt'; $extension = pathinfo($filename, PATHINFO_EXTENSION); if (!empty($extension)) { echo "The file extension is: $extension"; } else { echo "The file has no extension."; } ?>
extension_loaded() function
<?php $extension = 'mysqli'; if (extension_loaded($extension)) { echo "The $extension extension is loaded."; } else { echo "The $extension extension is not loaded."; } ?>
scandir() function
<?php $directory = '/path/to/directory'; $files = scandir($directory); if (count($files) <= 2) { echo "The directory is empty."; } else { echo "The directory is not empty."; } ?>
is_writable() function
<?php $directory = '/path/to/directory'; if (is_writable($directory)) { echo "The directory is writable."; } else { echo "The directory is not writable."; } ?>
<?php function isLastDayOfMonth($dateToCheck) { $dateObj = new DateTime($dateToCheck); $currentMonth = $dateObj->format('m'); $nextDay = clone $dateObj; $nextDay->modify('+1 day'); $nextMonth = $nextDay->format('m'); return $currentMonth !== $nextMonth; } $dateToCheck = '2024-04-30'; if (isLastDayOfMonth($dateToCheck)) { echo "The date $dateToCheck is the last day of the month."; } else { echo "The date $dateToCheck is not the last day of the month."; } ?>
<?php function isDateWithin30Days($dateToCheck) { $dateToCheckObj = new DateTime($dateToCheck); $thirtyDaysFromNow = new DateTime('+30 days'); return $dateToCheckObj <= $thirtyDaysFromNow; } $dateToCheck = '2024-04-18'; if (isDateWithin30Days($dateToCheck)) { echo "The date $dateToCheck is within the next 30 days."; } else { echo "The date $dateToCheck is not within the next 30 days."; } ?>
<?php function isDateGreaterThanToday($dateToCheck) { $dateToCheckObj = new DateTime($dateToCheck); $currentDateObj = new DateTime(); return $dateToCheckObj > $currentDateObj->setTime(0, 0, 0); } $dateToCheck = '2024-04-18'; if (isDateGreaterThanToday($dateToCheck)) { echo "The date $dateToCheck is greater than today."; } else { echo "The date $dateToCheck is not greater than today."; } ?>
<?php function isDateBeforeToday($dateToCheck) { $dateToCheckObj = new DateTime($dateToCheck); $currentDateObj = new DateTime(); return $dateToCheckObj < $currentDateObj->setTime(0, 0, 0); } $dateToCheck = '2024-04-18'; if (isDateBeforeToday($dateToCheck)) { echo "The date $dateToCheck is before today."; } else { echo "The date $dateToCheck is not before today."; } ?>