代码之家  ›  专栏  ›  技术社区  ›  ParoX

文件\u get\u contents create file不存在

php
  •  4
  • ParoX  · 技术社区  · 15 年前

    Download #: <?php $hits = file_get_contents("downloads.txt"); echo $hits; ?>
    

    然后在下载页面,我有这个。

    <?php
        function countdownload($filename) {
            if (file_exists($filename)) {
                $count = file_get_contents($filename);
                $handle = fopen($filename, "w") or die("can't open file");
                $count = $count + 1;
            } else {
                $handle = fopen($filename, "w") or die("can't open file");
                $count = 0; 
            }
            fwrite($handle, $count);
            fclose($handle);
        }
    
        $DownloadName = 'SRO.exe';
        $Version = '1';
        $NameVersion = $DownloadName . $Version;
    
        $Cookie = isset($_COOKIE[str_replace('.', '_', $NameVersion)]);
    
        if (!$Cookie) {
            countdownload("unqiue_downloads.txt");
            countdownload("unique_total_downloads.txt");
        } else {
            countdownload("downloads.txt");
            countdownload("total_download.txt");
        }
    
        echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$DownloadName.'" />';
    ?>
    

    当然,用户首先访问预下载页面,所以它还没有被创建。我不想添加任何功能到预下载页,我希望它是平原和简单的,而不是大量的添加/更改。

    编辑:

    $count = (file_exists($filename))? file_get_contents($filename) : 0; echo $count;
    
    3 回复  |  直到 12 年前
        1
  •  14
  •   Sergey Eremin    15 年前
    Download #: <?php
    $hits = '';
    $filename = "downloads.txt";
    if (file_exists($filename)) {
        $hits = file_get_contents($filename);
    } else {
        file_put_contents($filename, '');
    }
    echo $hits;
    ?>
    

    你也可以使用 fopen() “w+”模式:

    Download #: <?php
    $hits = 0;
    $filename = "downloads.txt";
    $h = fopen($filename,'w+');
    if (file_exists($filename)) {
        $hits = intval(fread($h, filesize($filename)));
    }
    fclose($h);
    echo $hits;
    ?>
    
        2
  •  2
  •   GDP Danny    12 年前

    像这样的字谜会导致疯狂的,不可预见的问题。要将字符串转换为整数,只需将整数0添加到任何字符串。

    例如:

    $f = file_get_contents('file.php');
    $f = $f + 0;
    echo is_int($f); //will return 1 for true
    

    但是,我建议使用数据库而不是文本文件。有几种方法可以解决。一种方法是每次有人下载文件时,在名为“download\u count”的表中插入一个唯一的字符串。查询就像“insert into download\u count$randomValue”一样简单-确保索引是唯一的。然后,在需要计数时,只需计算表中的行数。行数是下载计数。你有一个真正的整数,而不是一个假装是整数的字符串。或者在“下载文件”表中创建一个具有下载计数整数的字段。不管怎样,每个文件都应该在一个具有id的数据库中。当有人下载文件时,在下载函数中从数据库中提取该数字,将其放入变量、增量、更新表中,然后在客户机上以您想要的方式显示它。使用PHP和jqueryajax来异步更新它,使之更酷。

    如果您坚持使用文本文件,我仍然会使用php和jquery.load(file.php)。这样,您就可以使用文本文件存储任何类型的数据,只需使用上下文选择器加载文本文件的特定部分。php接受$GET请求,加载文件的正确部分并读取文件中存储的数字。然后它增加存储在文件中的数字,更新文件并将数据发送回客户机,以便以任何您想要的方式显示。例如,可以在文本文件中设置一个id为“downloadcount”的div,以及一个id为要存储在此文件中的任何其他数据的div。加载file.php时,只需将div#download#count与文件名一起发送,它将只加载存储在该div中的值。这是一种将php和jquery用于酷而简单的Ajax/数据驱动应用程序的绝妙方法。不是要将其转换为jquery线程,但这很简单。

        3
  •  0
  •   simhumileco Janarthanan Ramu    8 年前

    您可以使用更简洁的等效函数 :

    function countdownload($filename) {
    
        if (file_exists($filename)) {
    
            file_put_contents($filename, 0);
    
        } else {
    
            file_put_contents($filename, file_get_contents($filename) + 1);
        }
    }