
May 16th, 2007 by

Pete
In PHP you can provide file downloads by using a PHP page to “pass through” the file. Of course you could just link to the actual file, however this technique means your file can kept outside of your website root. Another advantage is you can integrate this system into a PHP based user/password system easily.
Now this technique is fairly straightforward, but you need to make sure you get the HTTP Headers correct for it to run smoothly. Here’s the code:
PHP:
-
$file = "/path/to/file.txt";
-
-
-
//This will set the Content-Type to the appropriate setting for the file
-
switch ($file_extension)
-
{
-
case "htm":
-
case "xhtml";
-
case "txt";
-
case "html": $ctype="text/html"; break;
-
case "pdf": $ctype="application/pdf"; break;
-
case "doc": $ctype="application/msword"; break;
-
case "exe": $ctype="application/octet-stream"; break;
-
case "zip": $ctype="application/zip"; break;
-
case "xls": $ctype="application/vnd.ms-excel"; break;
-
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
-
case "gif": $ctype="image/gif"; break;
-
case "png": $ctype="image/png"; break;
-
case "jpeg":
-
case "jpg": $ctype="image/jpeg"; break;
-
case "mp3": $ctype="audio/mpeg"; break;
-
case "wav": $ctype="audio/x-wav"; break;
-
case "mpeg":
-
case "mpg":
-
case "mpe": $ctype="video/mpeg"; break;
-
case "mov": $ctype="video/quicktime"; break;
-
case "avi": $ctype="video/x-msvideo"; break;
-
default: $ctype="application/force-download";
-
}
-
-
//Begin writing headers
-
-
-
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
-
header("Cache-Control: public");
-
header("Content-Description: File Transfer");
-
//Use the switch-generated Content-Type
-
header("Content-Type: $ctype");
-
-
-
// regex stops download prompt for browser associated types
-
if (!
preg_match('/(htm|xhtml|txt|html|gif|png|jpeg|jpg)/',
$file_extension,
$matches))
-
header("Content-Disposition: attachment; filename=\"".
basename($filename).
"\";" );
-
-
// output the file
-
-
I will be writing a follow up article in the future that shows how this technique can be improvement using Apache's mod-rewrite to provide cleaner URLs for file downloads.
Note: The regular expression on line 42 will make files normally associated with the browser (images, webpages, etc) open inside the browser. If you want to get the “open/save” download prompt for all types of files then comment out this line.
Note: Make sure you use: “Content-Type: image/jpeg” for JPGs, IE doesn’t seems to like “Content-Type: image/jpg”
# Pete Graham
Posted in Website Development, php, regular expressions, programming |
2 Comments »

May 15th, 2007 by

Pete
The other day I was programming in PHP and needed to get the first index in an associative array. There doesn’t appear to be a built in function to deal with this so I wrote my own.
PHP:
-
function getArrayFirstIndex($arr)
-
{
-
foreach ($arr as $key => $value)
-
return $key;
-
}
Here's a bit of code you can test it on:
PHP:
-
$arr['banana'] = "yellow";
-
$arr['apple'] = "apple";
-
$arr['orange'] = "orange";
-
-
$first_index = getArrayFirstIndex($arr);
-
-
echo "first index =".
$first_index;
The above code will output 'first index = banana'.
/* Pete Graham */
Posted in Website Development, php, programming |
4 Comments »

May 14th, 2007 by

Pete
Recently I’ve been making an effort to improve my JavaScript skills, I’m not a JavaScript-Jedi yet but I have been picking up a few, tips, tricks and techniques.
One thing that I find extremely useful is using an init function to call other functions when the page has loaded. I have been using the nifty function below:
JAVASCRIPT:
-
function WindowOnload(f)
-
{
-
var prev=window.onload;
-
window.onload=function(){ if(prev)prev(); f(); }
-
}
I save this function in a file called init.js which is then included in my HTML pages using:
HTML:
-
<script xsrc="/js/init.js" type="text/javascript"></script>
Functions can then be initialised by including the following in the HTML:
JAVASCRIPT:
-
<script language="JavaScript">
-
<!--
-
-
WindowOnload(function()
-
{
-
a_function('Sdelete_date');
-
});
-
-
WindowOnload(function()
-
{
-
another_function('a_parameter');
-
});
-
-
//-->
-
</script>
The above code is inline JavaScript which is often seen as being naughty, obviously you can save the same code to a separate file and include that instead.
I originally saw this technique here. Alternative JS initialisation scripts are available but so far this one has worked well for me, so I’ll be sticking with it.
# Pete Graham
Posted in javascript, Website Development, programming |
No Comments »

March 29th, 2007 by

Pete
Here's a nifty one liner in PHP which let's you swap 2 variables values.
Much like the PHP XOR swap there's no real reason this is any better than using a temporary variable, but you might be able to impress chicks with it or something.
/* Pete Graham */
Posted in Tech, php, programming |
4 Comments »

March 22nd, 2007 by

Pete
If you need to remove elements from an array that have a particular value(s) here’s a neat way of doing it without any looping:
PHP:
-
// our initial array
-
$arr =
Array("blue",
"green",
"red",
"yellow",
"green",
"orange",
"yellow",
"indigo",
"red");
-
-
-
// remove the elements who's values are yellow or red
-
-
-
-
// optionally you could reindex the array
-
-
This is the output from the code above:
Array ( [0] => blue [1] => green [2] => red [3] => yellow [4] => green [5] => orange [6] => yellow [7] => indigo [8] => red )
Array ( [0] => blue [1] => green [4] => green [5] => orange [7] => indigo )
Array ( [0] => blue [1] => green [2] => green [3] => orange [4] => indigo )
Pete Graham xXx
Posted in Tech, php, arrays, programming |
11 Comments »