Jump to content

Download file from server to server


killuagdt

Recommended Posts

I am currently using PHP 5.3.2 with IIS 7.5. I have just started learning PHP, and I am wondering how I can make a server to server download through PHP? For example I have a direct link like www.abc.com/folder/123.doc which link directly to the file 123.doc. I want to make my server to download the file above to my server to a directories like: /downloaded/123.doc. So could you please tell me how to do it? Thank you very much for your help.

Link to comment
Share on other sites

you can look at this example

 

$fp = fopen (dirname(__FILE__) . '/downloaded/my_file', 'w+');
$ch = curl_init('http://server.where.interesting/file.resides.com/123.doc');
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp)

 

 

pay attention to the curl_setopt() http://gt.php.net/manual/en/function.curl-setopt.php

 

optionally, at the begin of the script you can set

 

set_time_limit(0); // to prevent timeout for big file download
ini_set('display_errors',true);//useful for debug purposes

Link to comment
Share on other sites

@FuThAr: Thank you very much for your codes. I am currently at work so I could not check them but I will try as soon as possible. But I have a small question, is there any limit for the download like file size?

 

Providing url wrappers are enabled copy will suffice.

I am new to PHP so could you please be more detailed? :D. Thank you very much.

 

 

By the way, after googling for a while, I found that some people use fopen/fsockopen with fgets/fwrite for downloading from server to server. I have paid some time for the documentations but simply could not get the idea and could not understand some examples. So if anyone has time, could you please tell me a way to get file with fopen (as I search, fsockopen seems to be somewhat more difficult)? Is there any advantage of using fopen/fsockopen/curl? Your helps are greatly appreciated.

Link to comment
Share on other sites

UPDATE:

 

I have tried the code of FuThar and received good result for small file. But for big file (like over 100Mb), the script stop when the file has just been downloaded for more than 1Mb (maybe due to my slow internet connection, but the script runs for less than 1 minute):D. There is no error statement instead of Notice: Use of undefined constant __FILE_ - assumed '__FILE_'. So I am looking for help to fix this problem:D. Thank you very much in advance.

 

PS: I have already included

set_time_limit(0); 
ini_set('display_errors',true);

at the beginning of the script.

Link to comment
Share on other sites

I am sorry to double post but somehow I couldn't see the edit button anymore :confused: So if the mods look here, please merge my posts and accept my apology.

 

I could get over the php limit but now I am receiving Fast CGI process exceeded configured activity timeout error. So could anyone please show me a way to change the timeout? I am using IIS 7 on Windows 7.

 

PS: fcgiext.ini could not be found in my windows folder, so I think I can not change timeout through changing fcgiext.ini

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.