Reggie Thomson’s Diary

Diary of a Digital Photographer

August 31st, 2006

Download.php code for forcing download of mp3s jpegs

I have had several requests for the PHP code for forcing downloads of specific file types. Here it is:

<?php

function download_file ($file)
{
	//First, see if the file exists
	if (!is_file ($file))
	{
		die ('<b>404 File not found!</b>') ;
	}

	$file_length = filesize ($file) ;
	$filename = basename ($file) ;
	$file_extension = strtolower (substr (strrchr ($filename, '.'), 1)) ;
	$file_modified = filemtime ($file) ;

	//This will set the Content-Type to the appropriate setting for the file
	switch ($file_extension)
	{
	case 'kmz':
		$content_type = 'application/vnd.google-earth.kmz' ;
		break ;
	case 'kml':
		$content_type = 'application/vnd.google-earth.kml+xml' ;
		break ;
	case 'pdf':
		$content_type = 'application/pdf' ;
		break ;
	case 'exe':
		$content_type = 'application/octet-stream' ;
		break ;
	case 'zip':
		$content_type = 'application/zip' ;
		break ;
	case 'doc':
		$content_type = 'application/msword' ;
		break ;
	case 'xls':
		$content_type = 'application/vnd.ms-excel' ;
		break ;
	case 'ppt':
		$content_type = 'application/vnd.ms-powerpoint' ;
		break ;
	case 'gif':
		$content_type = 'image/gif' ;
		break ;
	case 'png':
		$content_type = 'image/png' ;
		break ;
	case 'jpeg':
	case 'jpg':
		$content_type = 'image/jpg' ;
		break ;
	case 'mp3':
		$content_type = 'audio/mpeg' ;
		break ;
	case 'wav':
		$content_type = 'audio/x-wav' ;
		break ;
	case 'mpeg':
	case 'mpg':
	case 'mpe':
		$content_type = 'video/mpeg' ;
		break ;
	case 'mov':
		$content_type = 'video/quicktime' ;
		break ;
	case 'avi':
		$content_type = 'video/x-msvideo' ;
		break ;

	//The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
	case 'php':
	case 'htm':
	case 'html':
	case 'txt':
		die ('<b>Cannot be used for '. $file_extension .' files!</b>') ;
		break;
	default:
		$content_type = 'application/force-download' ;
	}


	switch ($file_extension)
	{
	case 'kmz':
	case 'kml':
		header ('Last-Modified: ' . gmdate ('D, d M Y H:i:s \G\M\T', $file_modified));
		header ('Content-Length: ' . $file_length) ;
		header ('Content-Type: ' . $content_type) ;
		break ;

	default:
		//Begin writing headers
		header ('Pragma: public') ;
		header ('Expires: 0') ;
		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: ' . $content_type) ;

		//Force the download - set the headers
		header ('Content-Disposition: attachment; filename=' . $filename . ';') ;
		header ('Content-Transfer-Encoding: binary') ;
		header ('Content-Length: ' . $file_length) ;
	}

	//Now read the file and exit
	@readfile ($file) ;
	exit ;
}

// Put the file in a directory on your website and change the information below
// replacing "/directory/to/" with your directory
// and "filename.ext" with the filename and its extension

$file = '/directory/to/filename.ext' ;
download_file ($file) ;

?>

You can also view this as a text file from here.

August 20th, 2006

Godaddy is expiring .uk domain names before their expiry date

Copy of email sent to Godaddy, 2006-08-20 16:48 BST

Dear Sir/Madam,

The domain lawandethics.org.uk was purchased on the basis that it would forward all traffic and email to a specified address (lawandethics.org). The domain does not expire until 9/20/2006, yet it has now been redirected to a Godaddy advertising page.

If the redirection is not restored within 24 hours, legal action may proceed against Godaddy.

Any further domains that are automatically expired by Godaddy prior to their expiry date may also be subject to legal action.

[Update 2006-08-20] Godaddy replied:

Thank you for contacting Customer Support. The registries of these TLDs require that a registrant renew their domain name before the 20th day of the month prior to the expiration month. This policy is referenced in the domain registration agreement.

For Example:
If a .jp, .co.uk or .de domain expires 1/31/2006… it must be renewed before 12/20/2005.
If a .jp, .co.uk or .de domain expires 3/19/2005… it must be renewed before 2/20/2005.

You still have the option to have us renew it for you and bring it back to an active state, but at this time it is pending expiration. You may verify this in your account.

If you would like us to renew lawandethics.org.uk for you please reply with the last 4 digits of the payment method you would like to renew this domain on (it must be a valid payment method that is already on file in your Go Daddy customer account) and the length of time for which you would like us to renew it.

Please note that if this domain has privacy this will have to be renewed at the same time and for the same length of time as the domain name.

Please be aware that this process usually takes 24 hours. Please let us know if we can help you in any other way.

It would be more useful if Godaddy sent emails giving the Actual expiry date, rather than misleading information.

August 16th, 2006

Plesk backup to remote FTP

Plesk 8.01 is still giving problems with backup to remote FTP.

I use a Godaddy FTP server as backup. However, Plesk seems to delete the old backup file without creating a new one.

My workaround is to do backups manually.

Using Putty SSH, I create a backup directory, eg. 20060816.

Then, in Plesk -> Domains -> (domain name) -> Backup Manager -> FTP Account properties, I change the FTP directory to “/20060816″, do a manual backup to FTP, then change the FTP directory back to “/”

Maybe someday Plesk will be reliable enough to work automatically.

Also, the FTP repository is not visible as an automatic restore, which is a nuisance. I’m assuming it will be possible to use FTP to bring the backups into the correct location on my server to make them visible.

[Update 2006-12-16:] Plesk 8.1.0 now has an improved backup, allowing FTP access and showing all FTP backups. The backup also includes statistics.

|