Monday, July 6, 2009

Task in Interview for php programmer

create an application
User can browse the file from the disk.
Get the data from that file then
create a XML file that contains most number of words first. only 100 words. word is not less than 3 characters. following format



the
ram
....

Tuesday, March 24, 2009

Mail sending with attachment using PHP

//Gather file data
$filename = $_FILES['filename1']['name'];
$tmp = $_FILES['filename1']['tmp_name'];
$sep = md5(time());

//Get file contents
$fdata = chunk_split(base64_encode(file_get_contents($tmp))); //Encode data into text form

//Email To address
$to = "ramkrism@gmail.com";

//Determine mime type
$ext = explode('.', $filename);
$ext = $ext[1];

if($ext == "JPG" || $ext == "jpg" || $ext == "JPEG" || $ext == "jpeg") {
$mime_type = "image/jpeg";
}
elseif($ext == "gif" || $ext == "GIF") {
$mime_type = "image/gif";
}
else {
exit("Error: Wrong file type!");
}

//message
$message = "Email content";

// headers
$headers = "From: \"From Name\" MIME-Version: 1.0
Content-Type: Multipart/Mixed; boundary=\"$sep\"
charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
--$sep
Content-Type: $mime_type; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"$filename\"
$fdata --$sep";

$subject = "Email Subject Here";
if(mail($to, $subject, $message, $headers)){
echo 'Sent';
}
else {
echo 'Not Sent';
}
?>

Saturday, March 21, 2009

What is the difference between connect and pconnect in PHP?

mysql_connect opens up a database connection every time a page is loaded. mysql_pconnect opens up a connection, and keeps it open across multiple requests.

mysql_pconnect uses less resources, because it does not need to establish a database connection every time a page is loaded.

First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).

These type of links is therefore called 'persistent'.