Script - Function for download a file with php

From wiki.morphey.org

Jump to: navigation, search

This script can be useful to make a download side-file server by php without functions through shell_exec (a lot of times disabled on shared hosting).


 
<?
 
function download_php($file_source, $file_target) {
  $file_source = str_replace(' ', '%20', html_entity_decode($file_source));
  if (file_exists($file_target)) { chmod($file_target, 0777); }
  if (($rh = fopen($file_source, 'rb')) === FALSE) { return false; }
  if (($wh = fopen($file_target, 'wb')) === FALSE) { return false; }
  while (!feof($rh)) { if (fwrite($wh, fread($rh, 1024)) === FALSE) { fclose($rh); fclose($wh); return false; } }
  fclose($rh); fclose($wh);return true;
}
 
$url = "http://www.yoursite.com/yourfile.tar.gz";
$dst_file = "/home/youruser/public_html/yourfile.tar.gz";
 
if (download_php($url,$dst_file)) { echo "Ok"; } else { echo "error!"; }
 
?>
 
Personal tools