Page 1 of 1

Php force-download failed reading url

Posted: Fri Sep 23, 2005 12:28 am
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.

Posted: Fri Sep 23, 2005 12:34 am
by feyd
xpc is a windows network name.. which often isn't viable for a linux machine to interact with.. try using the IP.

still doesnt work

Posted: Fri Sep 23, 2005 2:28 pm
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 />

Posted: Fri Sep 23, 2005 3:33 pm
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..

it works

Posted: Fri Sep 23, 2005 4:31 pm
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);