PHP文件下载计数器无效链接,原因是字符?

PHP文件下载计数器无效链接,原因是字符?,php,Php,我有一个脚本,我想编辑。 它基本上是下载文件并计数,然后按预期显示。唯一的问题是,当我使用包含字符的文件时,它会抛出无效文件错误,而不是下载 例如,当我有一个名为(GREGZYY-SUCK-iT-AN-SEE-2O16.zip)的文件时,它会下载该文件。但是,当我有一个名为(RyanJones-F.N.T(用于GG,KyleMorris&Murdo.zip)16的文件时,它会抛出一个无效文件错误 以下是我拥有的文件和代码: index.php download.php files folder

我有一个脚本,我想编辑。 它基本上是下载文件并计数,然后按预期显示。唯一的问题是,当我使用包含字符的文件时,它会抛出
无效文件
错误,而不是下载

例如,当我有一个名为(GREGZYY-SUCK-iT-AN-SEE-2O16.zip)的文件时,它会下载该文件。但是,当我有一个名为(RyanJones-F.N.T(用于GG,KyleMorris&Murdo.zip)16的文件时,它会抛出一个
无效文件
错误

以下是我拥有的文件和代码:

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>
<?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!');
}
}
?>

(下载次数)
(下载次数)
下载.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>
<?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!');
}
}
?>

如果第二个是您遇到的问题,那么最后没有扩展。我不确定它是100%,但可能是一个可以查看的地方。是的,我相信这是我遇到问题的download.php。不抱歉,这个文件(GREGZYY-SUCK-it-AN-SEE-2O16.zip)最后有.zip,这个:(RyanJones-F.N.T(对于GG,KyleMorris&Murdo.zip)有16个。我也不确定括号的位置,但这可能是原因。哦,不,那只是因为我漏掉了它,它们都是zip文件,并且在我的文件上标记正确。我觉得这与utf字符集有关,但不是100%,我手头没有php开发环境,但如果今晚晚些时候无法解决这个问题,我会看一看。