Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download Files to web server from other site using php

is it possible to download file larger than 200 mb onto my web hosting directly so that i dont have to download that file to my computer and then upload using my ftp client. and as i am not using ssh i cannot use wget. i was thinking of php or per or cgi may be.. (open to all ideas..)

+==============+                                  +--------+
|  Big server  | -----------+                +--->|web host|
+==============+            |   +------+     |    +--------+
                            +-->| MyPC |-----+        |
                                +------+              |     +========+
                                                      +---->| client |
                                                            +========+

or

+============+
| Big Server | ---+
+============+    |                      +----------+
                  +--------------------->| Web Host |
                                         +----------+
                                            |
   +------+                                 |      +========+
   | MyPC |                                 +----->| client |
   +------+                                        +========+

plz help....

like image 493
voldyman Avatar asked Dec 27 '10 17:12

voldyman


3 Answers

For cURL

$url = "http://path.com/file.zip";
$fh = fopen(basename($url), "wb");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
curl_close($ch);
like image 153
Heiko Avatar answered Nov 19 '22 09:11

Heiko


in php the easiest is probably:

<?php
copy('http://server.com/big.file','/local/path/big.file');
?>

however you should be able to execute wget. especially if external fopen is deactivated on your server which is very likely

using php just like:

<?php 
chdir('/where/i/want/to/download/the/file/');
system('wget http://server.com/big.file');
?>

or

<?php
system('wget -O /where/i/want/to/save http://server.com/big.file');
?>

curl is another way. you can execute the shell command or use curl php.

also make sure the folder (or file) you want to download to is writeable

like image 44
The Surrican Avatar answered Nov 19 '22 09:11

The Surrican


With PHP you can download the file with this:

<?php
$in = fopen('http://example.com/', 'r');
$out = fopen('local-file', 'w');
while(!feof($in)) {
  $piece = fread($in, 2048);
  fwrite($out, $piece);
}
fclose($in);
fclose($out);
?>

This requires two things:

  • The local file must be writable by the web server
  • allow_url_fopen must be activated on the web server
like image 2
Emil Vikström Avatar answered Nov 19 '22 07:11

Emil Vikström