3

I have this code and it works but has a limitation only can copy files of 4.0mb, someone can help me to increase that size to copy files larger. thanks

<? 
  $archivo_origen=$_POST["origen"]; 
  $archivo_destino=$_POST["destino"];

  function descarga_archivo ($archivo_origen,$archivo_destino){ 
    $mi_curl = curl_init ($archivo_origen);  
    $fs_archivo = fopen ($archivo_destino, "w");  
    curl_setopt ($mi_curl, CURLOPT_FILE, $fs_archivo);  
    curl_setopt ($mi_curl, CURLOPT_HEADER, 0);  
    curl_exec ($mi_curl);  
    curl_close ($mi_curl);  
    fclose ($fs_archivo);  
  } 
  descarga_archivo($archivo_origen,$archivo_destino);
    header("location: index.php");


 ?> 
  • Instead of using CURL, you might try [`file_get_contents($url)`](http://php.net/manual/en/function.file-get-contents.php)? – rjz Aug 11 '12 at 05:05
  • This seems to be a solution: http://stackoverflow.com/questions/6409462/downloading-a-large-file-using-curl – Cyclonecode Aug 11 '12 at 05:11
  • the problem is that this code runs on a free hosting server and I not have all the features enabled, so I use cURL, however would appreciate an example of file_get_contents ($ url) to prove it. – HeribertoVJ Aug 11 '12 at 05:12

1 Answers1

0
$fs_origen = fopen($archivo_origen, 'r');
$fs_destino = fopen($archivo_destino, 'w');
while($data = fread($fs_origen, 4096)) fwrite($fs_destino, $data);
fclose($fs_origen);
fclose($fs_destino);

EDIT: replace stream_copy_to_stream with loop.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • with all the codes that I can use just copy 4.0mb, I do not know if it will be by the server configuration – HeribertoVJ Aug 11 '12 at 06:19
  • It sounds like both curl and stream_copy_to_stream run into php memory size limits. You may have to write your own loop using fread() and fwrite() in smaller chunks. – Barmar Aug 11 '12 at 06:24
  • if you could give me an example, I do not know much about php. Use this code to download files that are denied because I am from Cuba and my proxy does not allow downloading files *. exe, *. zip Thanks – HeribertoVJ Aug 11 '12 at 06:32
  • thanks for the help but I think due to some configuration of my hosting, only copy 4.0mb. Thanks for your time – HeribertoVJ Aug 11 '12 at 06:46
  • Sounds like the hosting server has a file size limit. They probably don't want you filling up their disk with junk, or using it to host lots of illegal downloads. – Barmar Aug 11 '12 at 06:56