Jump to content

Downloading from remote location


Vince889

Recommended Posts

I think I have an idea, but I just want to make sure.

How do I download files from remote servers?

 

Lets say I am running: http://host.com/download.php

I want my download.php script to fetch "super.jpg" from http://anotherhost.com/super.jpg

 

How would I do that? Would I have to use cURL or include()? Or some sort of fopen() function?

Ex:

<?

include("http://anotherhost.com/super.jpg");

?>

 

Mind you, I want this file (super.jpg) SAVED to myself. I don't want to just display it, I want to save it. Pretty much just copying a file from a remote location to my server.

Link to comment
https://forums.phpfreaks.com/topic/160658-downloading-from-remote-location/
Share on other sites

I just want to use the content.

"Mind you, I want this file (super.jpg) SAVED to myself. I don't want to just display it, I want to save it. Pretty much just copying a file from a remote location to my server."

 

I want to copy super.jpg from the remote host and save it to my server.

file_get_contents() and file_put_contents() should work:

 

<?php
//set user agent string
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10');
$url = 'http://anotherhost.com/super.jpg';
file_put_contents('local/dir/' . basename($url), file_get_contents($url));
?>

No problem. In case you're wondering why I'm setting the user agent string, it's because some sites check it, and try to disregard (automated) requests from scripts etc. The user agent string sent along PHP requests is blank by default (I believe), else "PHP" or similar.

Yes, lets say I had a list of URLs..

Would I just duplicate this over and over, changing the file name each time?

 

<?php
//set user agent string
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10');
$url = 'http://anotherhost.com/super.jpg';
file_put_contents('local/dir/' . basename($url), file_get_contents($url));
?>

No, you would use a loop :) Example:

 

<?php
//set user agent string
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10');
$urls = array('http://example.com/file1.jpg', 'http://example.com/file2.jpg', 'http://example.com/file3.jpg', 'http://example.com/file4.jpg');
foreach ($urls as $url) {
file_put_contents('local/dir/' . basename($url), file_get_contents($url));
}
?>

But reading many files will require a lot of memory and execution time. You can set these values at the top of your script if your host allows it:

 

//remove time limit
set_time_limit(0);
//set memory limit to 200 MB
ini_set('memory_limit', '200M');

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.