PHP: Download a file from web to local machine

21,282

Solution 1

I think (!) that you're a bit confused.

You mentioned

allows the user to download a selected file to a local drive.

But the path "C:\MyScans\\".$FileName is is the path on the webserver, not the path on the user's own computer.

After you do whatever to retrieve the desired file from the remote website:

  1. Create a file from it and redirect the user to the file by using header('Location: /path/to/file.txt');

  2. Insert the following header:

    header('Content-disposition: attachment; filename=path/to/file.txt');

It forces the user to download the file. And that's probably what you want to do

Note: I have used the extension txt, but you can use any extension

Solution 2

you can use php Curl:

<?php
    $url  = 'http://www.example.com/a-large-file.zip';
    $path = '/path/to/a-large-file.zip';

    $fp = fopen($path, 'w');

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);

    $data = curl_exec($ch);

    curl_close($ch);
    fclose($fp);
Share:
21,282
Hank
Author by

Hank

I'm a physician who does programming on the side, for my office web site and intranet, and various groups (websites for my college class, our Men's Club, several others).

Updated on February 19, 2020

Comments

  • Hank
    Hank about 4 years

    I have searched the web for 2 days and can not find the answer.

    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.

    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.

    When I echo the source file ($data), it displays the correct content.

    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.

    The source path ($path) is behind a login, but I am already logged in.

    Any thoughts on why this is failing to write the file?

    Thanks, Hank

    Code:

    $path = "https://.../Reports/ReportDetails/$FileName";
    /* echo "Downloading: $path"; */
    $data = file_get_contents($path); /* echo "$data"; */
    $dest = "C:\MyScans\\".$FileName; /* echo "<br />$dest"; */
    $fp = fopen($dest,'wb');
    if ( $fp === FALSE ) echo "<br />Error in fopen";
    $result = fwrite($fp,$data);
    if ( $result === FALSE ) echo "<br />Can not write to $dest";
    /* else echo "<br />$result bytes written"; */
    $result = fclose($fp); /* echo "<br />Close: $result"; */
    
    • craig1231
      craig1231 almost 12 years
      Does your server sit on your local machine?
  • Hank
    Hank almost 12 years
    Thanks, but this didn't do it either, but the next comment solved the problem - I was saving the file to my server, not to my local machine. (I had looked for the files there, but missed them.)
  • Hank
    Hank almost 12 years
    Thanks, this worked. Initially, I used the header lines with readfile(), but could not get it to work. Then I tried file_get_contents, which also seeme not to work. Then I tried the cURL solution above, which also seemed to fail. I had looked on the server for the files, but missed them. Your answer was perfect, in that it highlighted my problem. Then it was a simple matter to get the filename path correct. Thanks heaps. --Hank
  • Hank
    Hank almost 12 years
    But now I find that the files (usually .pdf), which are fine on the webserver, are corrupted on my machine. They display fine from the webserver, but I receive a 'file damaged and can't be repaired' message on my home machine. Also, the file size has increased from 44 KB to 57 KB. Could it be that it is adding header information to the file? (The same thing happened with a .docx file.)
  • Nadav S.
    Nadav S. almost 12 years
    @Hank I'm happy I helped you. your last problem should be another question. I think the problem is with the encoding. try to add utf8_encode().
  • Hank
    Hank almost 12 years
    Nadav, It turns out that the extra content added to the end of the downloaded file was the html code output to the web page after the file was transferred (I show the user a list of available files to download; they click one and it is downloaded, returning them to the list). I needed an ob_start() before and ob_end_flush() after for this to not happen. Thanks for your help. --Hank