Page 1 of 2

mp3/wav download

Posted: Tue May 24, 2011 1:19 pm
by ashwood
on my website when songs are uploaded the user can download. i have the download button and currently its linked just to the file so when i click it, it goes mysite/music/song.mp3 instead of the download box thingy that should could up giving the user the option where to save etc like in i.e if you get me

cheers
ash

Re: mp3/wav download

Posted: Tue May 24, 2011 1:53 pm
by jacek
You can force a download by sending some headers that I always forget. See http://uk.php.net/manual/en/function.header.php

Re: mp3/wav download

Posted: Tue May 24, 2011 1:55 pm
by bowersbros
for in the code section, have a look at the php library there is a function for force download in the files.class.inc.php file

Re: mp3/wav download

Posted: Thu May 26, 2011 4:56 pm
by ashwood
bowersbros wrote:for in the code section, have a look at the php library there is a function for force download in the files.class.inc.php file

where do i find this sorry?

Re: mp3/wav download

Posted: Thu May 26, 2011 5:10 pm
by jacek
ashwood wrote:where do i find this sorry?
http://betterphp.co.uk/board/viewtopic.php?f=5&t=99

8-) 8-)

Re: mp3/wav download

Posted: Thu May 26, 2011 8:11 pm
by ashwood
wheres the files.class.inc.php file, i casnt just see 2 links to download?

Re: mp3/wav download

Posted: Thu May 26, 2011 8:12 pm
by ashwood
ive got it but i cant find the code to force the download?

Re: mp3/wav download

Posted: Thu May 26, 2011 8:46 pm
by jacek
at the bottom of the files.class.inc.php file there are two functions the send some headers.

Re: mp3/wav download

Posted: Thu May 26, 2011 8:56 pm
by ashwood
$ext = end(explode('.', $path));

		if (empty($types[$ext]) === false){
			return $types[$ext];
		}

		return 'application/octet-stream';
	}

	// recursivly empties a folder and deletes it.
	public static function rmrf($path){
		if (is_dir($path) === false){
			return false;
		}

		$files = scandir($path);
		unset($files[0], $files[1]);

		foreach ($files as $file){
			if (is_dir($path . DIRECTORY_SEPARATOR . $file)){
				if (count(glob($path . DIRECTORY_SEPARATOR . $file . DIRECTORY_SEPARATOR . '*')) > 0){
					self::rmrf($path . DIRECTORY_SEPARATOR . $file);
				}else{
					rmdir($path . DIRECTORY_SEPARATOR . $file);
				}	
			}else{
				unlink($path . DIRECTORY_SEPARATOR . $file);
			}
		}

		rmdir($path);

		return true;
	}

}

?>
?/

Re: mp3/wav download

Posted: Thu May 26, 2011 8:58 pm
by jacek
:oops: http.class.inc.php

sorry :lol:

Re: mp3/wav download

Posted: Thu May 26, 2011 9:02 pm
by ashwood
	public static function force_download($path){
		if (file_exists($path) === false){ return false; }

		header('Content-Type: application/octetstream'); // <-- this is for stupid IE6
		header('Content-Type: application/octet-stream');
		header('Content-Description: File Transfer');
		header('Content-Disposition: attachment; filename="' . basename($path) . '"');
		header('Content-Length: ' . filesize($path));

		// php4 method used here because large files would use a lot or RAM with readfile.
		$file = fopen($path, 'rb');

		while (feof($file) === false){
			echo fread($file, 4096);
		}

		fclose($file);
	}

	// forces the download of raw data.
	public static function force_download_raw($name, $size, $data){
		header('Content-Type: application/octetstream'); // <-- this is for stupid IE6
		header('Content-Type: application/octet-stream');
		header('Content-Description: File Transfer');
		header("Content-Disposition: attachment; filename=\"{$name}\"");
		header("Content-Length: {$size}");

		echo $data;
	}

}

?>

so how do i use this for how i need it?

Re: mp3/wav download

Posted: Thu May 26, 2011 9:06 pm
by jacek
Well it shows you what headers you need to send and how to read out a file to the browser.

You need to create a php script that handles the downloads

downloads.php
$path = $_GET['path'];

header('Content-Type: application/octetstream'); // <-- this is for stupid IE6
header('Content-Type: application/octet-stream');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="' . basename($path) . '"');
header('Content-Length: ' . filesize($path));

// php4 method used here because large files would use a lot or RAM with readfile.
$file = fopen($path, 'rb');

while (feof($file) === false){
       echo fread($file, 4096);
}

fclose($file);
would be the most basic form.

then you link like.
<a href="download.php?path=folder/file.mp3">download</a>
You then need to make sure that people cant use the script to download random files from your server, but I will leave that to you ;)

Re: mp3/wav download

Posted: Thu May 26, 2011 9:19 pm
by ashwood
would that work what you posted?

Re: mp3/wav download

Posted: Thu May 26, 2011 9:21 pm
by jacek
ashwood wrote:would that work what you posted?
Should do.

But like I said, you need to make sure people cant use it to download random files.

If you store the mp3 info in a database you could use the row id as the $_GET variables, rather than the actual path, and them query the database for the file name.

Re: mp3/wav download

Posted: Thu May 26, 2011 9:32 pm
by ashwood
on the download link its like this..
         <?php
              if(isset($_SESSION['login_username']))
              {
                echo "<a href='index.php?act=download?path=music/".$song_url."'><img src='../styles/default/download_small.png' alt='Download' title='Download' /> </a>";
              }
              else
                echo "";
         ?>

Re: mp3/wav download

Posted: Thu May 26, 2011 9:33 pm
by ashwood
when i click the download icon.. page doesnt exist and in the url i get

http://aatm-online.co.uk/index.php?act= ... 001835.mp3

Re: mp3/wav download

Posted: Thu May 26, 2011 9:38 pm
by jacek
http://aatm-online.co.uk/index.php?act=download?path=music/1001835.mp3

as a URL makes no sense, you want

http://aatm-online.co.uk/index.php?act=download&path=music/1001835.mp3

EDIT: also I like your background ;)

Re: mp3/wav download

Posted: Thu May 26, 2011 10:02 pm
by ashwood
jacek wrote:
EDIT: also I like your background ;)
thanks ;) what u think of overall design?

and also... yeah that works now thanks :D

Re: mp3/wav download

Posted: Thu May 26, 2011 10:26 pm
by jacek
ashwood wrote:thanks ;) what u think of overall design?
the block yellow bits on the pages looks a bit cheap compared to the nice icons and other things.

Overall fairly nice, but could do with a bit more styling.
ashwood wrote:and also... yeah that works now thanks :D
cool ... cool, cool, cool. (community reference :))

Re: mp3/wav download

Posted: Sun May 29, 2011 10:08 pm
by ashwood
jacek wrote:
ashwood wrote:thanks ;) what u think of overall design?
the block yellow bits on the pages looks a bit cheap compared to the nice icons and other things.

Overall fairly nice, but could do with a bit more styling.
ashwood wrote:and also... yeah that works now thanks :D
cool ... cool, cool, cool. (community reference :))

block yellow?

Re: mp3/wav download

Posted: Mon May 30, 2011 8:33 am
by jacek
ashwood wrote:block yellow?
http://aatm-online.co.uk/index.php The Home box.