Belgin Fish
Belgin Fish

Reputation: 19827

Readfile issue PHP

I'm using the following :

@header("Cache-Control: no-cache, must-revalidate");
@header("Content-Type: application/octet-stream");
@header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
@header("Content-Length: ".$row['file_size']);
@header("Content-Disposition: attachment; filename=\"".$row['artist'] . " - " .$row['title']."\"");
@header("Content-type: audio/mpeg;\r\n");

to start a download, now for some reason its giving me

Warning: readfile(http://theurl.com/downloads/the file some spaces.mp3) [function.readfile]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /home/belgin/public_html/dl.php on line 29

now the url is valid, I've triple checked but could it be because there's spaces in the file name? And if so how can i fix that? Thanks

reafile line :

readfile(http://domain.com/downloads/Warp 1.9_The Bloody Beetroots_320000.mp3)

Upvotes: 1

Views: 5842

Answers (4)

3rdLion
3rdLion

Reputation: 159

urlencode() is not the way to go. As mentioned above, simply use str_replace().

Just another possibility here as to why its not working. This was the cause for me. Interestingly, file_exists was returning true but no form of serving the file to the public for download was working without having the below set correctly.

PHP has a setting called open_basedir

Make sure this is set correctly relevant to your hosting environment. open_basedir can be edited via php.ini

Upvotes: 0

Stichoza
Stichoza

Reputation: 4609

DO NOT encode url! It will encode like http%3A%2F%2Fexample.com%2Fsome%20song.mp3. You just need to replace spaces. so use str_replace instead...

readfile(str_replace(" ", "%20", $url));

Upvotes: 2

Ascherer
Ascherer

Reputation: 8083

I think your going to need to urlencode() and html_entities() the url or filename or whatever

Upvotes: 3

Vasilis Lourdas
Vasilis Lourdas

Reputation: 1179

It seems that the url contains spaces. Do you escape it properly for use inside readfile()?

Upvotes: 2

Related Questions