Jul 24, 2010
Delete Directory in PHP
This post will be helpful to those who work with directories and files.
I am writing here the PHP code for deleting the directory.
<?php
$extract_dir = 'path'; //physical path of the directory
if(deleteDirectory($extract_dir)) //delete directory
{
$msg = "$extract_dir was deleted";
echo $msg."<br/>";
}
else
{
$msg = "$extract_dir was not deleted";
echo...
read more
Apr 10, 2010
Passing by Reference
I am listing here a simple example to explain ‘passing by reference’ in PHP.
I hope it is helpful.
<?php
$a = 5;
echo 'A1->'.$a.'<br/>';
$b = 6;
$a =& $b;
echo 'A2->'.$a.'<br/>B1->'.$b.'<br/>';
global $var;
function foo(&$var)
{
$var++;
}
$a=5;
echo 'A3->'.$a.'<br/>';
foo($a);
echo 'A4->'.$a.'<br/>Var->'.$var;
?>
For...
read more
Mar 10, 2010
Insert and Retrieve file with MySQL
Some of you might be knowing that there is a functionality in MySQL database for storing files and retrieving the same.
This is mainly used when we need to reduce response time for retrieving and using files in our application.
I am listing here the method for inserting image file in and retrieving image file from MySQL database using PHP.
1) For inserting data into MySQL you need to convert the image...
read more
Feb 14, 2010
Mail in PHP
I am listing here the method for sending mail using PHP code. This will be useful in many ways, for example when you want to sent mail after say a form is submitted.
Note: This may work in local , but I am suggesting you to execute on live server.
<?php
ini_set("SMTP","mail.yoursitename.com"); //this is in general case, put here the SMTP address.
ini_set("SMTP_PORT",25);...
read more
Feb 10, 2010
Extracting String
This is small but very useful script.
I have shown example for extracting only ’src’ value from the ‘img’ tag, but you can modify and use as per your requirement using ‘preg_match_all’ function.
<?php
$src= '<img height="120" width="120" border="0" src="http://www.somesite.com/abc.jpg" class="image"/>';
preg_match_all('/(src)=("[^"]*")/i',$src,...
read more
Feb 10, 2010
Page Redirect
I have written a script for getting the files list from directory and redirecting the page on selecting option from the select box.
So you will get the script for fetching all the files from current directory and redirecting page on selection.
<h>Please select option</h>
<?php
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI']; //current directory physical path
$dir_handle =...
read more
Feb 1, 2010
Database Backup
I am listing here the PHP script to generate backup of database.
<?php
/* Database Backup */
$username = "dbusername"; //database username
$password = 'dbpass'; //database password
$hostname = "localhost"; //host
$dbname = "dbname"; //databse name
$sConnString = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$name...
read more
Jan 22, 2010
Get IP Address in PHP
Many times we need the IP Address of the User visiting our website in our Application.
I am listing here the method to get the User IP Address, hope it might be useful to many of you.
//function for getting IP
//start
function getuserip()
{
if (isset($_SERVER))
{
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
return $_SERVER["HTTP_X_FORWARDED_FOR"];
...
read more
Jan 21, 2010
Assign PHP Array to Javascript Array
I am describing here a very useful method for getting the values of a PHP array in JavaScript.
For this we need to assign the PHP array values to a array in JavaScript.
<?php
$a = array('A','B','C');
?>
<html>
<head>
<title>Assign PHP Array to Javascript Array</title>
<script language="javascript">
function showValues(){
var a=new Array;
<?php
for($i=0;$i<count($a);...
read more
Jan 20, 2010
Copy Directory
I am listing here the method to copy full Directories with all sub-directories and files.
<?php
$source = 'f1'; //folder name
$target = 'f2'; //folder name , if target folder does not exists, it will be created
//echo 'src->'.$source.'<br/>';
//echo 'tar->'.$target.'<br/>';
if(is_dir($source))
{
full_copy($source,$target);
echo 'Folder copied';
}
else
{
echo 'Not copied';
}
function...
read more
