Forum Moderators: coopster

Message Too Old, No Replies

Download a remote file via URL?

By calling wget?

         

iProgram

3:52 am on Aug 3, 2006 (gmt 0)

10+ Year Member



I have a web interface which lets users to upload files. Alternatly, I also want to let users to retrieve files remotely. For example, there is a PDF file at http://www.example.com/test.pdf, a user can provide this URL and press a button, and then a PHP script downloads this remote file to my server. So is there a stable script which can do this for me?

If I do this by myself, I will use exec('wget http://www.example.com/test.pdf --output-document=something.pdf'), is this reliable enough?

mcavic

5:25 am on Aug 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, wget is reliable. But remember to check the URL that the user enters to be sure it's safe. In particular, you should reject it if it contains any of ; ' " < > ( ) { } \

Also, PHP has a built-in version of Curl. It's the same basic idea as wget, and the built-in version takes more work to use, but is probably more robust.

iProgram

10:15 am on Aug 3, 2006 (gmt 0)

10+ Year Member



Thanks!

tata668

12:20 pm on Aug 3, 2006 (gmt 0)

10+ Year Member



copy [ca3.php.net] works with URLs...

vincevincevince

12:42 pm on Aug 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can also (if need be) do this:

$destination=fopen("files/localfile.dat","w");
$source=fopen($url,"r");
while ($a=fread($source,1024)) fwrite($destination,$a);
fclose($source);
fclose($destination);

The advantage of this method is that you can enforce maximum upload sizes without having to download everything before you check for the file size:


$destination=fopen("files/localfile.dat","w");
$source=fopen($url,"r");
$maxsize=3000;
$length=0;
while (($a=fread($source,1024))&&($length<$maxsize))
{
$length=$length+1024;
fwrite($destination,$a);
}
fclose($source);
fclose($destination);

Imagine your server has 3Gb space free and someone supplies you a 3.1Gb file by URL. Your server is going to have serious problems pretty soon if you just eat it all and then check.