mp3/wav download

Ask about a PHP problem here.
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

mp3/wav download

Post 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
I would put a signature.. BUT, i don't have the time.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: mp3/wav download

Post 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
Image
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: mp3/wav download

Post 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
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: mp3/wav download

Post 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?
I would put a signature.. BUT, i don't have the time.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: mp3/wav download

Post by jacek »

ashwood wrote:where do i find this sorry?
http://betterphp.co.uk/board/viewtopic.php?f=5&t=99

8-) 8-)
Image
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: mp3/wav download

Post by ashwood »

wheres the files.class.inc.php file, i casnt just see 2 links to download?
I would put a signature.. BUT, i don't have the time.
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: mp3/wav download

Post by ashwood »

ive got it but i cant find the code to force the download?
I would put a signature.. BUT, i don't have the time.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: mp3/wav download

Post by jacek »

at the bottom of the files.class.inc.php file there are two functions the send some headers.
Image
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: mp3/wav download

Post 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;
	}

}

?>
?/
I would put a signature.. BUT, i don't have the time.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: mp3/wav download

Post by jacek »

:oops: http.class.inc.php

sorry :lol:
Image
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: mp3/wav download

Post 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?
I would put a signature.. BUT, i don't have the time.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: mp3/wav download

Post 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 ;)
Image
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: mp3/wav download

Post by ashwood »

would that work what you posted?
I would put a signature.. BUT, i don't have the time.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: mp3/wav download

Post 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.
Image
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: mp3/wav download

Post 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 "";
         ?>
I would put a signature.. BUT, i don't have the time.
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: mp3/wav download

Post 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
I would put a signature.. BUT, i don't have the time.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: mp3/wav download

Post 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 ;)
Image
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: mp3/wav download

Post 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
I would put a signature.. BUT, i don't have the time.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: mp3/wav download

Post 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 :))
Image
ashwood
Posts: 144
Joined: Thu May 12, 2011 7:17 am

Re: mp3/wav download

Post 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?
I would put a signature.. BUT, i don't have the time.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: mp3/wav download

Post by jacek »

ashwood wrote:block yellow?
http://aatm-online.co.uk/index.php The Home box.
Image
Post Reply