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
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.

Link to comment
Share on other sites

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));
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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));
?>

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.