Php

Script to list directory and its files

List folder sub folder and its file

function ListFiles($dir) { if($dh = opendir($dir)) { $files = Array(); $inner_files = Array(); while($file = readdir($dh)) { if($file != "." && $file != ".." && $file[0] != '.') { if(is_dir($dir . "/" . $file)) { $inner_files = ListFiles($dir . "/" . $file); if(is_array($inner_files)) $files = array_merge($files, $inner_files); } else { array_push($files, $dir . "/" . $file); } } } closedir($dh); return $files; } } foreach (ListFiles('../modules') as  ..

Backup mysql database script

Automate export and import mysql database

<?php error_reporting(E_ALL & ~E_NOTICE); $hostname="localhost"; $username="root"; $password="mypass"; $dbname="backupdb"; backup_tables($hosthost,$username,$password,$dbname); function backup_tables($host,$user,$pass,$name,$tables = '*') { $link = mysql_connect($host,$user,$pass); mysql_select_db($name,$link); //get all of the tables if($tables == '*') { $tables = array(); $result = mysql_query('SHOW TABLES'); while($row = mysql_fetch_row($result)) { $tables[] = $row[0]; } } else { $tables = is_array($tables) ? $tables : explode(',',$tables); } foreach($tables as $table) { $result = mysql_query('SELECT * FROM '.$table); $n_fields = mysql_num_fields($result); $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); $return.= "\n\n".$row2[1].";\n\n"; for ($i = 0; $i < $n_fields;  ..

How to get ip address of a domain

Fetch IP-Address of a website

To get an IP of a website in php, generally we use, gethostbyname("example.com"); This will work nicely, but for some urls instead of returning IP of the given url this function outputs url what you typed. For example, we can try a url which is redirected to another url then  ..

How to use ctype function

Validate numeric or sting value

How to validate the given string using ctype function? Normally I used to use preg function to validate the string. Some days ago I rad in php manual that the preg function is slower than other validating function, but still has its own pons and cons. Today I came across a  ..

Progress bar script

Php progress in terminal using php

The below script will help you see progress bar on your terminal. When we are doing some big task and it seems that the processing time will take some time in the background then we can use the progress bar on foreground. The below code will show progress status from  ..

How to get page name using basename

Parse url and get the page name

How to get the page name from a url? To parse the url of the page you present in the sub-directory, Earlier I use to use the parse_url function to get current page. Today I experience a different method to extract the page name using another different function using basename. If  ..

Error php file get contents no images

File_get_contents does not display images

Some times file_get_contents($url) will not display images in the $url. This below code rectifies the issue. <?php $URL = "http://www.indiandir.com/addurl.php"; $domain = file_get_contents($URL); if($domain !== FALSE) { $domain = preg_replace('/<head>/i', "<head>\n<base href='$URL'>\n", $domain); echo $domain; } ?>   ..

Code to identify the mobile device

Mobile Redirection using php code

How to identify the mobile device using php code? The mobile device can be identified by using HTTP_USER_AGENT function, once the device is identified then it can be redirected to a different page which is compatible to mobile device. mobile = "http://mobile.MY_DOMAIN.com"; $text = $_SERVER['HTTP_USER_AGENT']; $var[0] = 'Mozilla/4.'; $var[1] = 'Mozilla/3.0'; $var[2] = 'AvantGo'; $var[3] = 'ProxiNet'; $var[4]  ..

Find memory usage of php script

Calculate memory usage of php script

At times you might have a requirement to find how much memory is consumed by your PHP script when the script is executed. You might require this information if your script has a memory leak or takes a long time to execute. If that's the case then you can use  ..

Simple if else code reduce coding lines

If else statement

In general we write if else as if($a) { print $b; } else { print $c; } Reduce the if else code by ? : print ( $a ? $b : $c );   ..

Trim white space in php

Trim function

Strip or trim whitespace in php using trim(). Remove unwanted space from your code. <?php $hello = " Hello w3calculator.com "; $trimm = trim($hello); echo $trimm; ?> Output: Hello w3calculator.com In the above output you can see the white space were removed.   ..

Get total number of rows from mysql table

Count the rows or data in mysql table

The below code will help you to get the total number of rows from the mysql table. It will just print the number of rows of a table. <?php $link = mysql_connect("localhost", "mysql_user", "mysql_pass"); mysql_select_db("database", $link); $result = mysql_query("SELECT * FROM table1", $link); $num_rows = mysql_num_rows($result); echo "$num_rows Rows\n"; ?>   ..

How to return filename component

Find extension of a file name

Return filename component of path The function basename() will return the base name of the file. $path = "/home/malu/html/index.php"; $file = basename($path); // $file is set to "index.php" echo "$file"; ?> The above code will display the base name with the extension. Result : index.php $path = "/home/malu/html/index.php"; $file = basename($path, ".php"); // $file is set to "index" echo "$file"; ?> The above  ..

How to print without using echo

Print a statement

When ever there is a need to print only a variable in php like <?php echo $val; ?> we can instead use <?= $val ?> Example:   ..

How to create a simple array

Php range function create array

The easiest way to create an alphabets is using range() function // to get a array of characters from a to f $a = range('a','f'); print_r($a); // to get a array of characters from d to h $b = range('d','h'); print_r($b); ?> Result: Array ( [0] => a [1] => b [2] => c [3] => d [4] =>  ..

Previous 1 2 3 4 5 Next

Tech Bluff