Search code examples
phpcodeigniterrssitunes

iTunes Podcast RSS PHP Tracking - Not Downloading


I am trying to track downloads of MP3 Podcasts in iTunes. I have created a simple codeignitor site which generates URLS like:

www.site.com/podcasts/download/[id].mp3

The script takes the ID out of the URL, checks it exists in the DB - Gets the URL of the files and then does the following:

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 01 Jan 2011 01:01:01 GMT"); 
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename=episode'.$id.'.mp3"');
readfile($this->podcast_model->get_strPodcastFileName());   

If you hit the URL in a browser it works and the file starts to download, but when putting 1 link into iTunes it starts saying "Downloading 0%" but does not download?

The 1 thing to note is the method: $this->podcast_model->get_strPodcastFileName() returns a HTTP:// link and not a link to a file on the server, also the HTTP address is not on the same server as the script. Although this is working when downloading the files via the browser as I said above.

Does anyone have any ideas?

Thanks James


Solution

  • I fixed it by instead of using readfile(...) I simply did a header 301 redirect to the URL like this:

    header("HTTP/1.1 301 Moved Permanently" ); 
    header("Location: http://www.url_to_mp3_file/1.mp3");  
    

    One point for anyone else who is trying this. I would suggest you do a check on database for previous downloads in last X minutes (I check again IP address, URL and timestamp) as some MP3/Audio players make several requests to buffer the file.

    Hope this helps.