Home » Php » curl – Download Files to web server from other site using php

curl – Download Files to web server from other site using php

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&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);

Answer:

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

Answer:

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