Monday, January 19, 2009

Exporting database table records into an Excel file Using PHP

Hey hero, The below php snippet helps to export a table records into an excel file. Juz copy & paste the below php script as 'export_to_excel.php'. All you need is, Just Point your 'export to excel' link to 'export_to_excel.php' or customize whatever you need. This'll work for you. [Functionality of some lines are commented below]

mysql_connect("localhost","root","") or die(mysql_error());//Database connection
mysql_select_db("jenson") or die(mysql_error());//database name jenson
$time=time();//for unique file name, I've used time() function [Return current Unix timestamp]
header("Content-type: application/vnd.ms-excel");//header setting for Microsoft Excel
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=Report_".$time.".xls");
header("Content-Transfer-Encoding: binary");

$header.="Name\tE-Mail\tAddress\tPhone";
$data=$header."\n\n";
$q=mysql_query("SELECT * FROM details");//Select contents from table details
while($r=mysql_fetch_array($q))
{
$name=stripslashes($r['name']);//table details field 1
$email=stripslashes($r['email']);////table details field 2
$addr=stripslashes($r['address']);//table details field 3
$phone=stripslashes($r['phone']);//table details field 4
$addr=str_replace("\r","",$addr);
$addr=str_replace("\n","",$addr);
$addr = strip_tags($addr,'
'
);

$line .="$name"."\t"."$email"."\t"."$addr"."\t"."$phone"."\t";//4 field values as a record in one line
$line .= "\n";
}

$data .= $line;
echo
$data;// printing each line into each row of an excel file

?>


Juz copy & paste the above php script as 'export_to_excel.php'. Follow the steps mentioned above. Happy coding, Enjoy!

Thursday, January 15, 2009

Force download dialogue box using PHP

Here is a PHP script for Opening a download dialogue box when clicks on a Download link. Save the below script as 'download.php' & Point your download link to 'download.php' & also Pass the name of the file to be downloaded to 'download.php'
For eg:
If file to be downloaded is jenson.mp3, then put a Download link as <a href="download.php?file=<?=$filename?>">Download</a> [$filename is the variable for 'jenson.mp3'] on any page. That'll work for you.


// downloading a file--JensON-- 
$filename "jenson/upload/".$_GET['file'];//$_GET is used for getting the filename 
//[jenson/upload/ is the directory where jenson.mp3 file stored] 
// fix for IE catching or PHP bug issue 
header("Pragma: public"); 
header("Expires: 0"); // set expiration time 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
// browser must download file from server instead of cache 

// force download dialog 
header("Content-Type: application/force-download"); 
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 

// use the Content-Disposition header to supply a recommended filename and 
// force the browser to display the save dialog. 
header("Content-Disposition: attachment; filename=".basename($filename).";"); 

/* 
The Content-transfer-encoding header should be binary, since the file will be read 
directly from the disk and the raw bytes passed to the downloading computer. 
The Content-length header is useful to set for downloads. The browser will be able to 
show a progress meter as a file downloads. The content-lenght can be determines by 
filesize function returns the size of a file. 
*/ 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".filesize($filename)); 

@
readfile($filename); 
exit(
0); 
?>


Juz copy & paste the above php script as 'download.php'. Follow the steps mentioned above. Happy coding, Enjoy!

Tuesday, January 13, 2009

Listing Directory/Subdirectories using PHP

Hey Hero, This is the First Post of this blog. The below PHP Script helps to list all directory/sub directories of a specified path. Here we go.....

function getDirectory$path '.'$level ){ 

$ignore = array( 'cgi-bin''.''..' ); 
// Directories to ignore when listing output. Many hosts 
// will deny PHP access to the cgi-bin. 

$dh = @opendir$path ); 
// Open the directory to the handle $dh 

while( false !== ( $file readdir$dh ) ) ){ 
// Loop through the directory 

if( !in_array$file$ignore ) ){ 
// Check that this file is not to be ignored 

$spaces str_repeat' ', ( $level ) ); 
// Just to add spacing to the list, to better 
// show the directory tree. 

if( is_dir"$path/$file" ) ){ 
// Its a directory, so we need to keep reading down... 

echo "$spaces $file"
getDirectory"$path/$file", ($level+1) ); 
// Re-call this same function but on a new directory. 
// this is what makes function recursive. 

} else { 
$fileformat=explode("."$file); 

echo 
"$spaces $file"
// Just print out the filename 






closedir$dh ); 
// Close the directory handle 


getDirectory("g:\jenson"); //jenson is the directory in G drive 

?>


Juz copy & paste this code wherever it needs. Happy coding, Enjoy!