How to download file from server using PHP Code

I am new to php. Can any body tell me how I can download a file from PHP code from any server.

Possible duplicate Download file with curl from php server [closed]

+3


source to share


1 answer


You can use Curl to download file from internet using php

function curl_get_file_contents($URL) {
  $c = curl_init();
  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($c, CURLOPT_URL, $URL);
  $contents = curl_exec($c);
  $err  = curl_getinfo($c,CURLINFO_HTTP_CODE);
  curl_close($c);
  if ($contents) return $contents;
  else return FALSE;
}

      

pass a link to this function and download the content. alternatively you can use a file reader / writer



private function downloadFile ($url, $path) {
  $newfname = $path;
  $file = fopen ($url, "rb");
  if ($file) {
    $newf = fopen ($newfname, "wb");
    if ($newf)
    while(!feof($file)) {
      fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
    }
  }
  if ($file) {
    fclose($file);
  }
  if ($newf) {
    fclose($newf);
  }
} 

      

from: This question is on the stack

+5


source







All Articles