I want to be able to download files from my server with php. It works so far so good, but only for files with text in it (.txt
.php
, so files with simple text (even if there I have an interesting phenomenon, always having one empty line before text starts... ideas why?), but when I try to download an .jpg
file or an .exe
it's not working at all (error when trying to open...)
Here is the code I used:
<?php
session_start();
$file = basename($_GET['file']);
$path = 'uploads/'.$_SESSION['userid']."/".$file;
?>
<?php
if(!file_exists($path)){
die("file not found");
} else {
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($path);
exit;
}