Category Archives: PHP

Import data into csv – php

<?php
mysql_connect(“localhost”,”root”,”password”);
mysql_select_db(“phpcodez”); // Dont forget to put correct database name
$filename = “users-csv”.date(‘-Y-M-d-D-H-i-s’).”.csv”;
$fp = fopen($filename, ‘w’) or die (‘file cant be opened’);

$fieldsQry = mysql_query(“SHOW COLUMNS FROM user”);
while ($fields = mysql_fetch_assoc($fieldsQry)) {
$fieldNames[] = $fields[‘Field’];
}
fputcsv($fp, $fieldNames);

$usersQry = mysql_query(“SELECT * FROM user”);
while($userInfo=mysql_fetch_assoc($usersQry)){
fputcsv($fp, $userInfo);$i++;
}

system(“chmod 777 $filename”);
fclose($fp);
echo “Imported “.$i.” items”
?>

Read csv file in php

<?php
$filename = str_replace(“.”,date(‘-Y-M-d-D-H-i-s.’),$_FILES[‘upload’][‘name’]);
echo “<table border=’1′>”;
if (($handle = fopen($filename, “r”)) !== FALSE) {
while (($data = fgetcsv($handle, 1000, “,”)) !== FALSE){ ?>
<tr><?php foreach($data as $values) {?><td > <?php echo $values ?></td><?php } ?></tr>
<?php }
}
echo “</table>”;
?>

PHP

PHP is a server side scripting language used in web development to cerate dynamic web pages and can be embedded into HTML codes .The web server with php processor module interprets the code and geneate the web pages as per the given code .

PHP is a loosely typed language. It means you do not have to tell PHP which data type the variable is.PHP automatically converts the variable to the correct data type, depending on its value.

PHP stands for PHP Hypertext Preprocessor (A recursive acronym) .Earlier it was known as personnel home pages .

PHP is an open source software and its free to download and use .

It supports many databases like MySql,Oracle etc…

Send mail with attachment – PHP

<?php

class sendMail{

function send($mailto, $subject,$message , $filename, $path, $from_mail, $from_name, $replyto ) {

if(!empty($filename)) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, “r”);
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$name = basename($file);
}

$uid = md5(uniqid(time()));

$header = “From: “.$from_name.” <“.$from_mail.”>rn”;
$header .= “Reply-To: “.$replyto.”rn”;
$header .= “MIME-Version: 1.0rn”;
$header .= “Content-Type: multipart/mixed; boundary=””.$uid.””rnrn”;
$header .= “This is a multi-part message in MIME format.rn”;
$header .= “–“.$uid.”rn”;
$header .= “Content-type: text/html; charset=iso-8859-1n”;
$header .= “Content-Transfer-Encoding: 7bitrnrn”;
$header .= $message.”rnrn”;
$header .= “–“.$uid.”rn”;

if(!empty($filename)) {
$header .= “Content-Type: application/octet-stream; name=””.$filename.””rn”; // use different content types here
$header .= “Content-Transfer-Encoding: base64rn”;
$header .= “Content-Disposition: attachment; filename=””.$filename.””rnrn”;
$header .= $content.”rnrn”;
$header .= “–“.$uid.”–“;
}

if (mail($mailto, $subject, “”, $header)) {
return 1;
} else {
return 0;
}
}

}

 

$mail = new sendMail();

$mail->send(“info@phpcodez.com”,”Test”,”Test” , “image1.jpg”,”../images/”,”newsletter@phpcodez.com”,”PHPCodez”,”newsletter@phpcodez.com”);

?>

var_export

It prints parsable string representation of a variable

Example
======

<?php
echo “<pre>”;
$array = array(“php”,”codez”, array(“1”, “1”, “7”),1,2);
var_export($array);
?>

Output
=====
array (
0 => ‘php’,
1 => ‘codez’,
2 =>
array (
0 => ‘1’,
1 => ‘1’,
2 => ‘7’,
),
3 => 1,
4 => 2,
)

var_dump

It displays structured information about one or more expressions .

Example
======

<?php
echo “<pre>”;
$array = array(“php”,”codez”, array(“1”, “1”, “7”),1,2);
var_dump($array);
?>

Output
=====
array(5) {
[0]=>
string(3) “php”
[1]=>
string(5) “codez”
[2]=>
array(3) {
[0]=>
string(1) “1”
[1]=>
string(1) “1”
[2]=>
string(1) “7”
}
[3]=>
int(1)
[4]=>
int(2)
}