Page 1 of 1

downloading a file from URL to my own website

Posted: Fri May 27, 2005 6:11 am
by mistert
Hello!

I was wondering if somebody could help me with a code.
I want to download a file from a webpage, for example from http://www.blabla.com/my file.txt
and place it on my webhotel for example /home/b4344/my_file.txt

I tried this code, but it doesn´t work:

Code: Select all

$file1 = fopen("http://www.blabla.com/my file.txt", "r");
$file2 = fopen("/home/b4344/my_file.txt", "w";

if (!copy($file1, $file2)) 
   {
       echo "failed to copy $file1...<br />\n";
   }
please help!

Regards,

Mark

Posted: Fri May 27, 2005 6:22 am
by Chris Corbyn
Please read the rules on posting code.

Side note: Missing closing ")".

Quick and dirty but not 100% reliable way.

Use file_get_contents() to grab the data. then use fwrite() to write to your server.

Code: Select all

$data = file_get_contents('http://www.google.com/');

if ($handle = fopen('./newfile.html', 'w+')) {
    if (fwrite($handle, $data)) {
        echo 'Data copied successfully';
    } else {
        echo 'Data could not be copied to file';
    }
    fclose($handle);
} else {
    echo  'Failed to create file';
}

Posted: Fri May 27, 2005 6:31 am
by mistert
THANKS a LOT! You are GREAT!!!

Posted: Fri May 27, 2005 6:45 am
by Chris Corbyn
mistert wrote:THANKS a LOT! You are GREAT!!!
Scary... I didn't think it was that exciting, or that I was that GREAT :lol: