PHP: Download a file from web to local machine -
i have searched web 2 days , can not find answer.
i trying create routine displays files on site control, , allows user download selected file local drive.
i using code below. when uncomment echo statements, displays correct source , destination directories, correct file size , echo after fclose displays true.
when echo source file ($data), displays correct content.
the $filename variable contains correct filename, either .doc/.docx or .pdf. have tested both , neither saves destination directory, or anywhere else on machine.
the source path ($path) behind login, logged in.
any thoughts on why failing write 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 $dest"; /* else echo "<br />$result bytes written"; */ $result = fclose($fp); /* echo "<br />close: $result"; */
i think (!) you're bit confused.
you mentioned
allows user download selected file local drive.
but path "c:\myscans\\".$filename
is path on webserver, not path on user's own computer.
after whatever retrieve desired file remote website:
- create file , redirect user file using
header('location: /path/to/file.txt');
-
insert following header:
header('content-disposition: attachment; filename=path/to/file.txt');
it forces user download file. , that's want do
note: have used extension txt
, can use extension
Comments
Post a Comment