Issue
Background
I have a PHP application running on a CentOS server. The front end is using an ajax call to hit a PHP script. The script returns a file from the server and downloads it to the client.
Problem
If the file has a #
in the name, the file fails to download.
Example
File#Name.pdf
= Does not download correctly
FileName.pdf
= Does download correctly
This is the PHP used to retrieve the files,
if( isset($_GET['path'])) {
$path = $_GET['path'];
if(!file_exists($path)) {
die('file not found');
} else {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$path);
header('Content-Length: '.filesize($path));
readfile($path);
ob_clean();
flush();
exit;
}
}
Question
Why is the file failing to download when the #
is in the name?
Solution
Ah, is the filename coming in from a GET request? If so the # is probably getting rewritten as a %23. Try
$path = urldecode($_GET['path']);
Answered By - Nathaniel Granor Answer Checked By - Pedro (WPSolving Volunteer)