Sunday, October 24, 2010

Create csv file in php

$data = "Id, FirstName, LastName, Email, Phone, ZipCode"."\n";
$data .= "1,'Test1','Test2','Test@gmail.com','546435','10001'"."\n";

$myFile = "userinfo.csv";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $data);
fclose($fh);

Saturday, October 9, 2010

difference between curl and file_get_contents

file_get_contents - It is a function to get the contents of a file(simply view source items i.e out put html file contents).

curl - It is a library to do more operations, for example get the contents like file_get_contents, sending and receiving data from one site to another site and it also supports different types of protocols like http, https, ftp, gopher, telnet, dict, file, and ldap. curl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading HTTP form based upload, proxies, cookies.

You can read more about these in http://php.net/

Monday, July 26, 2010

days difference in php

$date2 = '2010-06-20';
$date1 = '2010-06-24';
$days = (strtotime($date1) - strtotime($date2)) / (60 * 60 * 24);
echo $days;

some other ways are also there. can any one reply as a comments

Monday, July 19, 2010

Validate Image Using Javascript

function validate_image()
{
frmobj = document.frmRegistration; //Here "frmRegistration" is Form name

var image_file = frmobj.upload_file.value; /*Here "upload_file" is in this tag like input type="file" name="upload_file" */

var image_length = frmobj.upload_file.value.length;

var pos = image_file.lastIndexOf('.') + 1;

var ext = image_file.substring(pos, image_length);

var final_ext = ext.toLowerCase();

if(final_ext != 'jpg' && final_ext != 'jpeg' && final_ext != 'gif' && final_ext != 'png')
{
alert('Upload valid image');
return false;
}
return true;
}
Usage:
if(!validate_image()) return false;

Sunday, July 18, 2010

create dynamic images in php

//Create Image By Url Method - 2
function create_image_by_url2($url)
{
$path_parts = pathinfo($url);
copy($url,$path_parts['basename']);
}
create_image_by_url2($url);

create dynamic images using php

$url = "http://technmarketing.com/wp-content/uploads/2009/03/twitter-wallpaper2.jpg";

//Create Image By Url Method - 1
function create_image_by_url1($url)
{
$target_path = $url;

$imgdir="";

$path_parts = pathinfo($url);

$file_name = $path_parts['basename'];

$imgext = $path_parts['extension'];

if($imgext=='jpg' || $imgext=='JPG')
$src_img=imagecreatefromjpeg($target_path);
if($imgext=='gif' || $imgext=='GIF')
$src_img=imagecreatefromgif($target_path);
if($imgext=='png' || $imgext=='PNG')
$src_img=imagecreatefrompng($target_path);
if($imgext=='jpeg' || $imgext=='jpeg')
$src_img=imagecreatefromjpeg($target_path);

$size = getimagesize($target_path);

$dst_img = imagecreatetruecolor($size[0],$size[1]);

imagecopyresampled($dst_img,$src_img,0,0,0,0,$size[0],$size[1],$size[0],$size[1]);

if(($imgext=='jpg') || ($imgext=='JPG') || ($imgext=='jpeg') || ($imgext=='JPEG'))
{
imagejpeg($dst_img, $imgdir.$file_name,90);
}else if(($imgext=='gif') || ($imgext=='GIF')){
imagegif($dst_img, $imgdir.$file_name,90);
}else if(($imgext=='png') || ($imgext=='PNG')){
imagepng($dst_img, $imgdir.$file_name,5);
}
}
create_image_by_url1($url);