the red penguin
HOME ABOUT SITEMAP BLOG LOGIN

General PHP code snippets

A collection of bits and pieces in PHP

Where am I?
Read all files in directory into array
Set timezone to local
Using mobile or tablet?


Where am I?

exec('pwd',$return);
print_r($return);

Read all files in directory into array

I couldn’t get scandir working – maybe a problem with permissions? But this one does work …

$dir   = 'subdirectory/subsubdir/etc';
$files = [];

if ($handle = opendir($dir)) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            array_push($files, $entry);
        }
    }
    closedir($handle);
}

Set timezone to local

To set the timezone to Europe/London you need to do this:

<?
date_default_timezone_set('Europe/London');
?>

Using mobile or tablet?

This function checks to see if the user is using a mobile phone or tablet without the need for media queries in CSS. Was working in 2022.

function isMobileDevice() {
    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
    }
}

Thursday 17 March 2022, 436 views


Leave a Reply

Your email address will not be published. Required fields are marked *