0

I try to download files uploaded to my server, but after download, they are empty, name and ending are correct, but not the rest. I try downloading it with that script:

<?php

session_start();

$file = basename($_GET['file']);
$path = 'uploads/'.$_SESSION['id']."/".$file;

if(!$path){ // file does not exist
    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;
}

I also tried multiple types of header but it doesn't seem to work.. Files to downnload can be all types, txt-files, pictures, .exe-files, etc.

FYI: When uploading the files they are saved in folders named with the user-id, that's how the path comes about. And the path seems to be completely right.

The file I try to download (as test-file) is a .txt-file, when opening it in the browser (....com/uploads/23/test.txt) I get the content, so it's not empty.

Does anybody have an idea, what the problem could be?...

nameless
  • 1,483
  • 5
  • 32
  • 78
  • What $file value you are getting? – Vishnu Sharma Apr 09 '16 at 08:08
  • try to check with [file_exists](http://php.net/manual/en/function.file-exists.php) function whether file exist or not. – Vishnu Sharma Apr 09 '16 at 08:11
  • $file-value is exactly the name of the file, for example `test.txt` No duplicate, I know this thread, but it doesn't change my problem. You're right, `if(!file_exists($path){die()}`makes it dying... What could the problem be? – nameless Apr 09 '16 at 08:15

0 Answers0