downloading a file from URL to my own website

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
mistert
Forum Newbie
Posts: 2
Joined: Fri May 27, 2005 4:37 am

downloading a file from URL to my own website

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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';
}
mistert
Forum Newbie
Posts: 2
Joined: Fri May 27, 2005 4:37 am

Post by mistert »

THANKS a LOT! You are GREAT!!!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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:
Post Reply