I've made a small PHP script which is supposed to allow visitors to download files from my server. The way it works is it takes one of the files, gets the basename and sends the headers as well as the file to the client.
However, when downloading a file I sometimes get index.php and sometimes get proper_file_name.zip.
I don't really know why it sometimes works and why it sometimes doesn't work. Any advice would be highly appreciated.
This is the relevant part of my source code:
// Client requested custom byte range
if(isset($_SERVER['HTTP_RANGE']))
{
$range = explode('-', substr($_SERVER['HTTP_RANGE'], 6));
$seekStart = intval($range[0]);
if ($range[1] > 0)
$seekEnd = intval($range[1]);
header('HTTP/1.1 206 Partial Content');
header(sprintf('Content-Range: bytes %d-%d/%d', $seekStart, $seekEnd, $size_vfile));
}
else // Set headers for full file
header('HTTP/1.1 200 OK');
// Get basename of filename
$filename = basename($filename);
// Send headers to client
header('Cache-Control: private');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"$filename\"");
header('Content-Transfer-Encoding: binary');
header('Content-Description: File Transfer');
header('Content-Length: '.$file_size);
header('Accept-Ranges: bytes');
$filename = basename($filename);- please show us the preceding code, where this is fixed up. If$filenameis empty, you end up with exactly this behaviour!