Monday, 26 October 2009

Mail2Blogger formatting

One of the really neat features in Blogger is the ability to create a blog entry simply by sending an email - then subject line becomes the title, the main body of the email becomes the content.


But looking at the output, I found some annoying formatting issues. Somewhere in the process, line-return characters were being added - resulting in a much narrower column for emailed entries than for normally-created entries.

And whilst the first section of content was delivered as a <div>, subsequent paragraphs were delivered as <p> paragraphs. The solution is to replace the opening <p> tag with two line-returns, and to ignore the closing </p> tag.

If you are using the output in an external page, and have PHP on your server, then for modest overhead, you can do some simple replacement to tidy up the output. Luckily, the line-returns added (at least in my case, from Outlook 2000) are plain <br> tags, whereas deliberately-added line-returns in Blogger are <br /> - so the text can be tidied up in a single pass like this:
$findArray=Array("<br>","<p>","</p>");
$replaceArray=Array(" ","<br /><br />","");
$blogHTML=file_get_contents(filePath("/Members/blog/blog.htm"));
$blogHTML=str_replace($findArray,$replaceArray,$blogHTML);
echo $blogHTML;

An extension of this could take care of fixed-format "signature" text added at the end of your emails (such as "sent from my iPhone") - but it might take a bit more programming to take care of any variable text (such as ads) that may be added by some email services.

Monday, 19 October 2009

Deleting a chosen file with PHP

I wanted a simple way to let users delete files already uploaded - and decided that I could create a form and populate it using PHP : one row per file, with a submit button each. The name of the submit button would be the file name. That way, when the form is submitted, I would get the key for the button pressed (each with value "Delete" so simply look for $_POST['Delete'], and delete that file.

Two snags worth noting:

First, PHP replaces a period OR a space with an underscore in field names submitted. so "abc def.pdf" appears in the $_POST array as "abc_def_pdf". The solution is to rawurlencode() the name and use this as the button name. Then when processing the $_POST field, use rawurldecode() - which still leaves the period as an underscore, so then run a replace function.


Secondly, there are lots of helpful scripts to show how to unlink, but none of them seemed to be clear on when a path is needed.
So if you write:
$handler = opendir($directory);
while ($fileName = readdir($handler)) {

the $filename variable will show just the filename - but when you come to
unlink($fileName);
it won't work - you need:
unlink($path.$fileName);

Example:


function modifyFileName($fileNameWithUnderscore) {
if (substr_count($fileNameWithUnderscore,"_") > 0) {
$lastPeriod = strrpos($fileNameWithUnderscore,"_");
$fileNameWithUnderscore = substr_replace($fileNameWithUnderscore,".",$lastPeriod,1);
}
return $fileNameWithUnderscore;
}


$hit=false;
$deletedOK=false;//default
foreach ($_POST as $key=>$value){
if ($value=="Delete"){
$encodedFileNameWithUnderscore=$key;
$hit=true;
}
}
if ($hit==true){
// create a handler for the directory
//$fileNameWithPeriod=modifyFileName($fileNameWithUnderscore);
$fileNameWithPeriod=modifyFileName(rawurldecode($encodedFileNameWithUnderscore));
$handler = opendir($directory);
// keep going until all files in directory have been read

while ($loopFileName = readdir($handler)) {
// if $file isn't this directory or its parent,
// delete it
if ($loopFileName != '.' && $loopFileName != '..'){
//echo $file;

//open and close to make sure it can be deleted
if ($loopFileName==$fileNameWithPeriod){
$fh = fopen($fileNameWithPeriod, 'w') or die("can't open file");
fclose($fh);
//delete the file
$deletedOK=unlink($directory.$fileNameWithPeriod);
}

} else {

}
}
// tidy up: close the handler
closedir($handler);
}