title: PHP: Download a file from web to local machine
body: <p>I have searched the web for 2 days and can not find the answer.</p>
  
  <p>I am trying to create a routine which displays the files on a site I control, and allows the user to download a selected file to a local drive.</p>
  
  <p>I am using the code below. When I uncomment the echo statements, it displays the correct source and destination directories, the correct file size and the echo after the fclose displays TRUE.</p>
  
  <p>When I echo the source file ($data), it displays the correct content.</p>
  
  <p>The $FileName variable contains the correct filename, which is either .doc/.docx or .pdf. I have tested both and neither saves anything into the destination directory, or anywhere else on my machine.</p>
  
  <p>The source path ($path) is behind a login, but I am already logged in.</p>
  
  <p>Any thoughts on why this is failing to write the file?</p>
  
  <p>Thanks,
  Hank</p>
  
  <p>Code:</p>
  
  <pre><code>$path = "https://.../Reports/ReportDetails/$FileName";
  /* echo "Downloading: $path"; */
  $data = file_get_contents($path); /* echo "$data"; */
  $dest = "C:\MyScans\\".$FileName; /* echo "&lt;br /&gt;$dest"; */
  $fp = fopen($dest,'wb');
  if ( $fp === FALSE ) echo "&lt;br /&gt;Error in fopen";
  $result = fwrite($fp,$data);
  if ( $result === FALSE ) echo "&lt;br /&gt;Can not write to $dest";
  /* else echo "&lt;br /&gt;$result bytes written"; */
  $result = fclose($fp); /* echo "&lt;br /&gt;Close: $result"; */
  </code></pre>