要将我正在使用GET请求的文件下载到下载服务器,例如domain.com/?file=/uploads/files/03/48/51/2017/04/09/file.rar
在我的电脑上,与大多数用户一样,下载顺利。但对于一些用户(约占总数的10-20%),存在错误。 从他们的话(不同的用户):
404错误
function clean($string) {
// $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
//$string = preg_replace('/[^A-Za-z0-9\-\.\_]/', '', $string); // Removes special chars.
return preg_replace('/\.\./', '', $string); // Replaces multiple hyphens with single one.
}
function file_force_download($file) {
$file = clean($file);
if (file_exists($file)) {
if (ob_get_level()) {
ob_end_clean();
}
if ($mimetype = mime_content_type($file)) {} else $mimetype="application/octet-stream";
header('Content-Description: File Transfer');
header('Content-Type:.'.$mimetype);
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
if ($fd = fopen($file, 'rb')) {
while (!feof($fd)) {
print fread($fd, 1024);
}
fclose($fd);
}
exit("Error while reading file");
} exit("File does not exist");
}
$file = $_GET['file'];
$file = preg_replace('/uploads\/files\//', '', $file);
if (preg_match("/\d\d\/\d\d\/\d\d\/\d\d\d\d\/\d\d\/\d\d\/.{1,60}/i", $file)) {
file_force_download("uploads/files/".$file);
} else exit ("Incorrect file link");
?>
我尝试更改下载服务器,但域无效。为什么会这样?