Category : "Php Snippets"

Php code snippets and examples

PHP best way to check if file is an image

The getimagesize function of php provides lot of information about an image file , including its type. The type can be used to check if the file is a valid image file or not. To check if a file is an image or not, use the function function is_image($path) { $a = getimagesize($path); $image_type = $a; if(in_array($image_type , array(IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP))) { return true; } return false; } $a ...

PHP create nested directories for a given path

If a file is to be saved in at path /var/www/a/b/c/d/abc.zip where the directory c and d dont exist then the directories have to created. Here is a function that uses recursion to check for directories in a path and create them if they do not exist : /** Make a nested path , creating directories down the path Recursion !! */ function make_path($path) { $dir = pathinfo($path ...

Set mysql timezone at runtime via PHP

To set the timezone of mysql at runtime via php use the following code : $c = mysqli_connect($host, $username, $password); mysqli_query($c , "SET `time_zone` = '".date('P')."'"); This can be executed when the connection is established and it will set the timezone of mysql to whatever timezone php is set to. Fast and effective. ...

PHP download remote file via url

The following function will download and save a remote file , using its http url : function download_remote($url , $save_path) { $f = fopen( $save_path , 'w+'); $handle = fopen($url , "rb"); while (!feof($handle)) { $contents = fread($handle, 8192); fwrite($f , $contents); } fclose($handle); fclose($f); } The method reads 8192 bytes at a time , and not the whole file , since if the whole file is read at once , then ...

PHP redirect – go back to previous page

To go back to the previous page the superglobal variable $_SERVER can be used. $_SERVER has the link to the previous page. So to redirect simply : #Method to go to previous page function goback() { header("Location: {$_SERVER}"); exit; } goback(); ...

PHP Format numbers to Indian Numerical System

The numerical system used in India differs from the Western system in terms of the placement of the thousands separator. Example : Number -> 1000000 Indian System -> 10,00,000 (Ten Lakh) Western System -> 1,000,000 (1 Million) While echoing numbers in PHP it is possible to format numbers with such separators using functions like number_format. To format a number in the Indian Numerical System ...

Php – parse text and convert urls into hyperlinks

The following function will parse a given text and convert all the urls into links. It does this using regular expressions. It converts email addresses to mailto links as well. Code function parse_links($str) { $str = str_replace('www.', 'http://www.', $str); $str = preg_replace('|http://(+)|', '<a href="http://$1">$1</a>', $str); $str = preg_replace('/((+)(.+)*@(+.)+{2,6})/', '<a href="mailto:$1">$1</a>', $str); ...

Pages:«123


Connect with us