我有一个我想编辑的脚本。
它基本上下载文件并对其进行计数,然后按预期显示它。唯一的问题是,当我使用包含字符的文件时,会抛出invalid file
错误而不是下载。
例如,当我有一个名为(GREGZYY-SUCK-iT-AN-SEE-2O16.zip)的文件时,它会下载该文件。但是,当我有一个名为(RyanJones - F.N.T(For GG,KyleMorris& Murdo.zip)16)的文件时,它会引发invalid file
错误。
以下是我拥有的文件和代码:
index.php
download.php
files folder & a counters folder
index.php如下:
<?php
function get_download_count($file=null){
$counters = './counters/';
if($file == null) return 0;
$count = 0;
if(file_exists($counters.md5($file).'_counter.txt')){
$fp = fopen($counters.md5($file).'_counter.txt', "r");
$count = fread($fp, 1024);
fclose($fp);
}else{
$fp = fopen($counters.md5($file).'_counter.txt', "w+");
fwrite($fp, $count);
fclose($fp);
}
return $count;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>
<a href="./download.php?file=filenameziphere.zip">filenameziphere</a> (Downloaded <?php echo get_download_count('filenameziphere.zip');?> times) <br>
<a href="./download.php?file=filenameziphere2.zip">filenamehere</a> (Downloaded <?php echo get_download_count('filenameziphere2.zip');?> times) <br>
</body>
</html>
download.php如下:
<?php
$downloads_folder = './';
$counters_folder = './counters/';
if(!empty($_GET['file'])){
$file = basename($_GET['file']);
if(file_exists($downloads_folder.$file)){
if(file_exists($counters_folder.md5($file).'_counter.txt')){
$fp = fopen($counters_folder.md5($file).'_counter.txt', "r");
$count = fread($fp, 1024);
fclose($fp);
$fp = fopen($counters_folder.md5($file).'_counter.txt', "w");
fwrite($fp, $count + 1);
fclose($fp);
}else{
$fp = fopen($counters_folder.md5($file).'_counter.txt', "w+");
fwrite($fp, 1);
fclose($fp);
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre- check=0');
header('Pragma: public');
header('Content-Length: ' . sprintf("%u", filesize($downloads_folder.$file)));
// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$file."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".sprintf($downloads_folder.$file));
ob_end_flush();
$fh = fopen($downloads_folder.$file, "rb");
while (!feof($fh)) {
echo fgets($fh);
flush();
}
fclose($fh);
exit;
}else{
header("HTTP/1.0 404 Not Found");
exit('File not found!');
}
}
?>
我尝试有时将utf charset
更改为utf-8
和其他部分,但仍然没有任何效果。任何帮助表示赞赏,如果可能的话,还需要更短的代码。