PHP Force Download

April 22nd, 2008
So today I needed to force a file to download in a web browser where the browser would normally just output the file. It will also work in IE6 over a secure (HTTPS) connection.. which was a bit like hard work.

function forceDownload($file,$name=false,$contenttype="application/octet-stream",$delete=false){
	if (!$name) $name = $file;
	
	header("Cache-Control: public, must-revalidate");
	header("Pragma: hack");
	header("Content-Type: " . $contenttype);
	header("Content-Length: " .(string)(filesize($file)) );
	header('Content-Disposition: attachment; filename="'.basename($name).'"');
	header("Content-Transfer-Encoding: binary\n");
					  
	$fp = fopen($file, 'rb');
	$buffer = fread($fp, filesize($file));
	fclose ($fp);
	echo $buffer;
	if ($delete){
		unlink($file);
	}
}

Example Usage:
So I wanted to create a temporary file, write some contents to it then force the file to be outputted via the browser and deleted on completion of download.
$data = 'what a load of content';    // data to write to a file

$filename = 'cache/tmpfile.txt';     // the file to write the data too, must be writable
file_put_contents($filename,$data);  // write the data to the file

// force the download and delete the file afterwards
forceDownload($filename,'whateverfilenameyoulike.txt','text/plain',true);

Flashbacks Of A Fool

April 18th, 2008
Flashbacks of a FoolA really good, touching British Movie, set mostly in the 70's this is about Joe Scott a failing Hollywood actor played by Daniel Craig who "sees the light" (so cliche), but in spite of that this is a really unique film that gets you thinking.

The director (Baillie Walsh) obviously knew his market perfectly as the first 15 minutes are basically just Daniel Craig walking around naked. Fortunately this doesn't last throughout the whole film.

Special note deserves to go to Felicity Jones who plays Ruth Davies, a young sweetheart who Joe Scott (Harry Eden (Young Daniel Craig)) falls for. Her acting is great and she, along with the awesome soundtrack really expresses what the 70's must have felt for teens, glam make-up and a very funny Bowie scene.

Flashbacks of a Fool is one of those films that really makes you appreciate being young, and if your of an older generation than myself would probably bring back some good memories.

Got to be a 7/10 from me.

Worlds Best PHP Function

April 15th, 2008
pr($array)
Yes, I know wht you're thinking, how can Scrivna have written the ultimate time saving function? Well to be honest... I haven't... someone else did it and I've ripped it off :-)

Basically, what really annoys me is that when your writing code with big arrays of data and you need to view that data, print_r is your friend, it outputs an array to the page, only problem is it doesn't output it as html, the most common way to remedy this is to either...

A) View the source of the page so it appears formatted correctly
B) Wrap your print_r statement in <pre> tags
C) Do nothing and struggle like hell to read the output
D) Use my fabtabulous function below (trust me, this will save you hours)

And now for the big moment... here it comes.. are you ready for this?

Wait for it, drum roll please....
function pr($arrayness){
	echo '<pre>';
	print_r($arrayness);
	echo '</pre>';
}

Da na!! Yup, isn't it great!

Not only is it shorter than typing print_r all the bloody time, it also formats your output properly.
If you really are crazy you could do some kind of crazy merge with the function over HERE to buffer the output and put it into a variable... but that's for another day.

Thankyou and goodnight.

Official UK Top 40 Singles Chart (RSS Feed)

April 14th, 2008
So having got frustrated with their being no Official UK Top 40 Singles or Album chart RSS feeds I decided i'd make my own...

Singles Chart or Album Chart

It's currently just a page scrape of the BBC Charts page so it might not be that reliable.. or that comprehensive.

Microwaves are Stupid!

April 14th, 2008
Yes people, it's true!

So you put your hamster in the microwave, turn the dial to 2 minutes, full power, hit start...

2 minutes later...
*Beep Beep Beep Beep Beep Beep*
Meanwhile...
You opened the door after the first beep... you quite obviously know it's finished... why the need for all the other beeps?!?!?!? Ahhhhh it drives me crazy!!!

New levels of grossness

April 12th, 2008
Do not watch this video under any circumstances!

http://www.morningstarr.co.uk/forum/underworld/5714-2-girls-1-cup.html

Having seen it i am scarred forever.

Javascript expanding textareas

April 12th, 2008
Hey ho,

All over the web i'm seeing sites such as facebook with textareas that vertically expand automagically (and from what I hear FF3 has this functionality built in) but until everyone starts using Firefox i'v written a handy javascript function that will simulate the effect in other browsers.

The problem with all the others examples i've seen round the net are that they all require you to use some sort of CSS bodgery or require jQuery or Prototype. With my lil sript theres no need for any of that nonsense and you can basically throw anything you like at it... or use your own styles in a stylesheet or inline.

I've tried to make it as easy as possible, all you need to do is add "autoExpand" to the textareas class and my script will do its magic, it can also works with ajax calls by calling autoExpand(object) after your ajax call returns.

The javascript file is available below and there is a DEMO you can have a looksy at.

It's been tested on IE7, FF2, Safari 3 and Opera 9 and works more or less as intended, it doesn't work on IE6 at the moment, but when i get access to IE6 i'll be sure to fix it :-)

Download: autoexpand.js

Love you!

Son of Rambow

April 10th, 2008





So went to see Son of Rambow tonight, it's a British film and lives up to all classic British film requirements; its a nice story about 2 friends growing up in the 80's who decide to make a comical action film.

There were a few moments in the film which I loved my favourite being a scene in a school sixth form common room where a load of kids are sitting around sniffing stuff that smells like cake... what's that all about??

What did surprise me about the film was that one of the boys comes from a Plymouth Brethren religious family. whcih you don't see very often... Needless to say the film doesn't make you want to convert.

The only real beef i've got with it is it was hyped up too much, i didn't find it as funny as i'd hoped and it was a bit soppy.. but that's just me being fussy.

All in all, I would recommend it and it gets a solid 7/10 form me.

Youtube Embed Rewriting

April 6th, 2008
So the time may come when you have code for embedding a youtube video into a webpage but need that video to be automagically resized and looping and putting the kettle on for you. Well... here's a snippet of code that just might do that for you!
<?php
// function to adjust a youtube videos settings given the embed code

function youtube_adjust($string,$width=425,$height=355,$autoplay=false,$looping=false){
	// match the url in the embed string

	$pattern = '/name="movie" value[\\s=\'"]+([^"\'>\\s]+)/is';
	preg_match($pattern,$string,$url);
	// split the url to get the different parts
	$result = split('[/&]', $url[0]);
	$id = $result[4];

	// shall i adjust these things?
	$autoplay = $autoplay ? 1 : 0;
	$looping = $looping ? 1 : 0; 

	// insert the new settings into the new format string and return it
	return '<a href="http://www.youtube.com/v/%27.$id.%27&hl=en&autoplay=%27.$autoplay.%27&loop=%27.$looping.%27" style="left: 0px ! important; top: 22px ! important" title="Click here to block this object with Adblock Plus" class="abp-objtab-06839183377036349 visible ontop"></a><a href="http://www.youtube.com/v/%27.$id.%27&hl=en&autoplay=%27.$autoplay.%27&loop=%27.$looping.%27" style="left: 0px ! important; top: 22px ! important" title="Click here to block this object with Adblock Plus" class="abp-objtab-010214352500039581 visible ontop"></a><object height="'.$height.'" width="'.$width.'">
<param name="movie" value="http://www.youtube.com/v/'.$id.'&hl=en&autoplay=1"></param>
<param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'.$id.'&hl=en&autoplay='.$autoplay.'&loop='.$looping.'" type="application/x-shockwave-flash" wmode="transparent" height="'.$height.'" width="'.$width.'"></embed></object>';
}

// this is the string youtube give you
$string = '<a href="http://www.youtube.com/v/iy3T3sp4EW4&hl=en" style="left: 0px ! important; top: 22px ! important" title="Click here to block this object with Adblock Plus" class="abp-objtab-06839183377036349 visible ontop"></a><a href="http://www.youtube.com/v/iy3T3sp4EW4&hl=en" style="left: 0px ! important; top: 22px ! important" title="Click here to block this object with Adblock Plus" class="abp-objtab-010214352500039581 visible ontop"></a><object height="355" width="425">
<param name="movie" value="http://www.youtube.com/v/iy3T3sp4EW4&hl=en"></param>
<param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/iy3T3sp4EW4&hl=en" type="application/x-shockwave-flash" wmode="transparent" height="355" width="425"></embed></object>';

// call and output it yar
echo youtube_adjust($string,200,100,true,true);
?>


P.S Wordpress is gay and likes inserting random character places, sorry if this has screwed up the code above.

Modified Preorder Tree Traversal (MPTT)

April 4th, 2008
Just stumbled upon this method of storing hierarchal data in a flat database and all I can say is its bloomin genius, it will change my life forever; no more crappy inefficient recursive functions.. I could explain it... but why bother when there's a great post accessible here.. you might find it hard to get your head around it at first, but trust me, you'll see the light.

MPTT

.