Category : "PHP"

Php tutorials

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 ...

Check if php is running from cli (command line)

Php from command line PHP scripts are usually opened in a web browser. But they can be run from command line or terminal as well. The syntax to run a script from commandline is very much similar to python or perl. php /path/to/script/index.php Inside a script it might be necessary to test if it is being run from command line or not. For ...

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 ...

Resize large images in php with imagemagick

Resizing images Web applications often need to work with images like pictures of users or photos etc. So the need to need to resize images comes up when creating thumbnails for example or compressing images for storage. The default library to handle image operations in php is GD. It can be used to resize images. Resize with GD The function imagecopyresampled is used ...

Check system load in php on linux

System load System load is a measure of how busy the system is running the processes. Higher system load simply means more processes are running and more are waiting to be run. When coding php applications, it sometimes is useful or necessary to find the current load on the server/system. For example if you want to run a long resource intensive ...

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: Get visitor’s location, city, country from IP address

Ip to Location Many applications need to get information about the geographical location of the user/visitor on their websites. This can be done in many ways. There are online services like ipinfodb and geoio that provide web based api to get the geolocation information about a given ip address. Services like Maxmind geoip provide free database that can be installed on ...

Parse and change date format in php

Parsing dates Most php applications need to parse dates and convert them to timestamps for example. Parsing is also necessary to convert the date into a more standard format like yyyy-mm-dd which can be stored in a database like mysql. Mysql database stores dates in the format yyyy-mm-dd like 2009-04-15. However humans are more used to the formats like dd-mm-yyyy or mm-dd-yyyy ...



Connect with us