Embbeding Image, Files within HTML like email

Wednesday, March 12, 2008



<?php

/**
* Embeding Image, Files within HTML like email.
*
* We Embed file within html code , so it is available within page itself not seperate file.
* Example 1: Image embeding
* Example 2: CSV file embeding
* Example 3: Download String as CSV
*/

error_reporting(E_ALL);

$def_img = 'http://www.google.co.in/images/nav_logo3.png';
$file = empty($_GET['f']) ? $def_img : $_GET['f'];

echo '<p>Example 1:</p>';
echo '<img src="'.embedImg($file).'" border="1" /><br> ';

function embedImg($path){
//$path = image path
//usage echo '<img src="'.embedImg($_GET['f']).'" border="1" width="50" height="50" /> ';
$pi = @pathinfo($path);

$ext = empty($pi['extension']) ? 'png' : $pi['extension'];

return "data:image/{$ext};base64,".@base64_encode( @file_get_contents($path) );
}



echo '<p>Example 2:</p>';

echo '<a href="'.downloadFile('csv.csv').'">Download CSV</a><br>';

function downloadFile($path){

#$path is path of any file
#usage echo '<a href="'.downloadFile('csv.csv').'">Download CSV</a>';
#echo '<a href="'.downloadFile('format.php').'">Download CSV</a>';

$pi = @pathinfo($path);
$ext = empty($pi['extension']) ? 'txt' : $pi['extension'];

return "data:application/{$ext};base64,".@base64_encode( @file_get_contents($path) );
}



echo '<p>Example 3:</p>';

$data_str = "col1,col2,col3\n1,2,3\na,b,c";
echo '<a href="' . downloadStrAsFile('a.csv',$data_str) . '"> Using string </a><br>';

function downloadStrAsFile($name, $strdata, $ext='csv'){
#echo '<a href="' . downloadAsFile('a.csv','a,b,c,d,e,f') . '"> Using string </a>';

$pi = @pathinfo($path);
return "data:application/{$ext};base64,".@base64_encode($strdata);

}

?>


0 comments:

Diseño original por Open Media | Adaptación a Blogger por Blog and Web