$f = empty($_GET['f']) ? "" : $_GET['f'];
if( file_exists($f ) ){
$f = realpath($f);
$cmd = " php -s $f ";
#echo $cmd;
exec($cmd, $op);
echo implode("\r\n", $op) ;
}else{
echo "Requested file not found : $f ";
}
Usefull PHP Command line Switches
Thursday, March 13, 2008Posted by Hemant Patel at 9:15 AM 0 comments
Usefull JavaScript Function's
String.prototype.isYMD = function (spliter){
//validate date y-m-d format
spliter = spliter || '-'; //defult value
var parts = (this + spliter + spliter ).split(spliter);
parts[0] = parseInt(parts[0], 10); //radix 10 required to handle 0 padded value
parts[1] = parseInt(parts[1], 10) -1; //month starts from 0
parts[2] = parseInt(parts[2], 10);
var dt = new Date( parts[0], parts[1], parts[2]);
var ret = (parts[0] == dt.getFullYear()) && (parts[1] == dt.getMonth()) && (parts[2] == dt.getDate() );
dt = null; parts = null
return ret;
//return parts.join('-') + ' : ' + dt.getFullYear() + "-" + dt.getMonth() + '-' + dt.getDay();
}
function compareYMD(d1, d2, spliter){
//compare two dateYMD
//return 0 if both equal, 1 if d1 heigher, -1 if d2 is higher
spliter = spliter || '-'; //defult value
d1 = (d1 + spliter + spliter).split(spliter);
d2 = (d2 + spliter + spliter).split(spliter);
d1[0] = parseInt(d1[0], 10);
d1[1] = parseInt(d1[1], 10);
d1[2] = parseInt(d1[2], 10);
d2[0] = parseInt(d2[0], 10);
d2[1] = parseInt(d2[1], 10);
d2[2] = parseInt(d2[2], 10);
ret = 0;
if( d1[0] > d2[0] ){
ret = 1
}else if( d1[0] <>0] ){
ret = -1;
}else if( d1[1] > d2[1] ){
ret = 1
}else if( d1[1] <>1] ){
ret = -1;
}else if( d1[2] > d2[2] ){
ret = 1
}else if( d1[2] <>2] ){
ret = -1;
}
return ret;
}
Posted by Hemant Patel at 9:11 AM 0 comments
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);
}
?>
Posted by Hemant Patel at 7:53 AM 0 comments
Download Partial HTTP data using PHP
/**
* Download Partial/Partof Page
*
* HTTP HEADERS links http://www.cs.tut.fi/~jkorpela/http.html
* HTTP RFC http://www.w3.org/Protocols/rfc2616/rfc2616.html
* Any Suggestion Welcome :)
*/
error_reporting(E_ALL);
$host = 'www.w3.org';
$path = '/2005/10/Process-20051014/activities.html';
$service_port = getservbyname('www', 'tcp');
$address = gethostbyname($host);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ( $socket === false ) {
die("socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n");
}
$result = socket_connect($socket, $address, $service_port);
if ( $result === false ) {
die("socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n");
}
$in = '';
$in .= "GET {$path} HTTP/1.1\r\n";
$in .= "Host: {$host}\r\n";
$in .= "Range: bytes=0-200\r\n"; //0 to 200 OR first 200
#$in .= "Range: bytes=200-400\r\n"; //200 to 400
#$in .= "Range: bytes=200-\r\n"; //200 to onwards
#$in .= "Range: bytes=-200\r\n"; //last 200
$in .= "Connection: Close\r\n\r\n";
socket_write( $socket, $in, strlen( $in ) );
while ( $out = socket_read( $socket, 2048 ) ) {
$parts = explode("\n\n", str_replace("\r", '', $out));
$responsebody = htmlentities( empty($parts[1]) ? "" : $parts[1] );
echo "<fieldset><legend>Request Header</legend>".nl2br($in)."</fieldset>";
echo "<fieldset><legend>Response Header</legend>".nl2br($parts[0])."</fieldset>";
echo "<fieldset><legend>Response Body</legend><textarea cols='80' rows='20'>{$responsebody}</textarea></fieldset>";
}
#echo "Closing socket...";
socket_close($socket);
?&>
Posted by Hemant Patel at 7:32 AM 0 comments
Subscribe to:
Posts (Atom)