Php force-download failed reading url

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
justquick
Forum Newbie
Posts: 5
Joined: Sun Jul 10, 2005 10:05 am

Php force-download failed reading url

Post by justquick »

I have recently migrated my webserver to linux and in the process readfile does not work for urls anymore. I have a file called forcedownload.php which takes $filename and $name and downloads the filename url with the desired name. it looks like

Code: Select all

header("Pragma: public"); 
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); 
header("Content-Type: application/force-download");

header("Content-Disposition: attachment; filename=\"".$name."\";" );
header("Content-Transfer-Encoding: binary");

readfile("$filename");
exit();
The resulting file only contains the following errors:

Code: Select all

<b>Warning</b>:  readfile() [<a href='function.readfile'>function.readfile</a>]: php_network_getaddresses: getaddrinfo failed: Name or service not known in <b>/opt/lampp/htdocs/forcedownload.php</b> on line <b>15</b>
<b>Warning</b>:  readfile(http://xpc/12-i_wayne-living_in_love-jah.mp3) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: Resource temporarily unavailable in <b>/opt/lampp/htdocs/forcedownload.php</b> on line <b>15</b>
The url is valid and once again this worked on my previous windows instillation perfectly.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

xpc is a windows network name.. which often isn't viable for a linux machine to interact with.. try using the IP.
justquick
Forum Newbie
Posts: 5
Joined: Sun Jul 10, 2005 10:05 am

still doesnt work

Post by justquick »

That seemed to fix it. It works ok for every url that is in the root folder of the remote url but nothing in any subdirectories. it returns

Code: Select all

<br />
<b>Warning</b>:  readfile(http://10.107.1.117/Gorillaz/Gorillaz/14 Slow Country.mp3) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: HTTP request failed! <head>
 in <b>/opt/lampp/htdocs/forcedownload.php</b> on line <b>15</b><br />
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try encoding the URL... php may not like the spaces and other "special" characters used in filenames..

have a look at parse_url() and rawurlencode().. you may require some fancy explode() or preg_split() work..
justquick
Forum Newbie
Posts: 5
Joined: Sun Jul 10, 2005 10:05 am

it works

Post by justquick »

yes that did it. I had to modify the url before downloading it, like so:

Code: Select all

$filename=split('%3A%2F%2F', $filename);
$filename=join('://',$filename);
$filename=split('%2f',$filename);
$filename=join('/',$filename);
Post Reply