Tuesday, January 11, 2011

Rewrite php file extensions to HTML

Most of the developers like to show PHP files as HTML. This can be done via htaccess. .htaccess files (or “distributed configuration files”) provide a way to make configuration changes on a per-directory basis. In htaccess, we have Rewrites. Htaccess Rewrites are enabled by using the Apache module mod_rewrite, which is one of the most powerful Apache modules and features availale. Htaccess Rewrites through mod_rewrite provide the special ability to Rewrite requests internally as well as Redirect request externally. So, if URL comes like http://www.vijayakumar.org/example.html. This URL will be rewritten as http://www.vijayakumar.org/example.php. To do this, we want to place the below line into htaccess, which was presented in root directory(htaccess will be hidden, try accessing hidden files in your root directory. If not presented?, please create a new htaccess file in root directory).

RewriteRule (.*)\.html $1.php

Friday, January 7, 2011

Send mail with attachment

function

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
echo = "mail send ... OK"; // or use booleans here
} else {
echo = "mail send ... ERROR!";
}
}

function to sent mail with attachment

$my_file = "somefile.zip";
$my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
$my_name = "Olaf Lederer";
$my_mail = "my@mail.com";
$my_replyto = "my_reply_to@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);

Unzip recursive

$file = "lightbox.zip";
$dir = getcwd();
function Unzip($dir, $file, $destiny="")
{
$dir .= DIRECTORY_SEPARATOR;
$path_file = $dir . $file;
$zip = zip_open($path_file);
$_tmp = array();
$count=0;
if ($zip)
{
while ($zip_entry = zip_read($zip))
{
$_tmp[$count]["filename"] = zip_entry_name($zip_entry);
$_tmp[$count]["stored_filename"] = zip_entry_name($zip_entry);
$_tmp[$count]["size"] = zip_entry_filesize($zip_entry);
$_tmp[$count]["compressed_size"] = zip_entry_compressedsize($zip_entry);
$_tmp[$count]["mtime"] = "";
$_tmp[$count]["comment"] = "";
$_tmp[$count]["folder"] = dirname(zip_entry_name($zip_entry));
$_tmp[$count]["index"] = $count;
$_tmp[$count]["status"] = "ok";
$_tmp[$count]["method"] = zip_entry_compressionmethod($zip_entry);

if (zip_entry_open($zip, $zip_entry, "r"))
{
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
if($destiny)
{
$path_file = str_replace("/",DIRECTORY_SEPARATOR, $destiny . zip_entry_name($zip_entry));
}
else
{
$path_file = str_replace("/",DIRECTORY_SEPARATOR, $dir . zip_entry_name($zip_entry));
}
$new_dir = dirname($path_file);

// Create Recursive Directory
$oldumask = umask(0);
@mkdir($new_dir, 0777);


$fp = @fopen($dir . zip_entry_name($zip_entry), "w+");
fwrite($fp, $buf);
@fclose($fp);
umask($oldumask);
zip_entry_close($zip_entry);
}
echo "\n";
$count++;
}

zip_close($zip);
}
}
Unzip($dir,$file);

Thursday, January 6, 2011

Unzip files - Zip_open

Simple script to unzip files using zip_open functions

$filename = basename("check1.zip");
$name = explode('.',$filename);

$zip = zip_open("check1.zip");
if ($zip) {
while ($zip_entry = zip_read($zip)) {
$oldumask = umask(0);
@mkdir($name[0], 0777);
umask($oldumask);
$fp = @fopen(zip_entry_name($zip_entry), "w+");
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
@fwrite($fp,"$buf");
zip_entry_close($zip_entry);
@fclose($fp);
}
}
zip_close($zip);
}