file_get_contents创建文件不存在

6
有没有替代file_get_contents的方法,可以在文件不存在时创建文件。我基本上是在寻找一行命令。我正在使用它来统计程序的下载统计信息。我在预下载页面中使用这个PHP代码:
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;

你的最后一行代码(或者更简短的版本:echo (int) @file_get_contents($file);)应该可以工作。你具体遇到了什么问题? - Victor Nicollet
正如Victor所建议的(通过类型转换),file_get_contents()将返回一个字符串,因此您应该将其转换为int以确保。 - MrWhite
3个回答

16
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;
?>

这比在下载页面为我的函数创建句柄之类的东西要好,但我希望有一个简短而简单的解决方案,但我想我只需要在预下载中拥有一个大而长的代码来计算下载次数。 - ParoX
现在我想起来了,你计算下载量的方式相当奇怪;) 为什么不使用数据库呢? - Sergey Eremin
只需要一个非常简单的解决方案。 - ParoX

2

像这样的类型转换可能会导致后来出现疯狂的、意想不到的问题。要将字符串转换为整数,只需将整数0添加到任何字符串中即可。

例如:

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

然而,我赞成使用数据库而不是文本文件来完成这个任务。有几种方法可以实现。其中一种方法是在名为“download_count”的表中每次有人下载文件时插入一个唯一的字符串。查询很简单,只需要“insert into download_count $randomValue” - 确保索引是唯一的。然后,在需要计算总数时,只需计算此表中的行数即可得到下载量。这样你就拥有了一个真正的整数,而不是一个伪装成整数的字符串。或者在“download file”表中创建一个包含下载计数整数的字段。无论如何,每个文件都应该有一个id存储在数据库中。当有人下载文件时,在下载函数中从数据库中获取该数字,将其放入变量中,进行递增、更新表并以任何你想要的方式显示在客户端上。使用PHP和jQuery Ajax异步更新它,使它看起来更酷。
如果你坚持使用文本文件,我仍然会使用php和jquery.load(file.php)。这样,你可以使用文本文件存储任何类型的数据,并使用上下文选择器仅加载文本文件的特定部分。file.php接受$_GET请求,加载文件的正确部分并读取存储在文件中的数字。然后,它递增存储在文件中的数字,更新文件并将数据发送回客户端以便以任何你想要的方式显示。例如,你可以在文本文件中有一个id设置为“downloadcount”的div和一个id用于存储此文件中其他数据的div。当你加载file.php时,只需发送div#download_count以及文件名,它将仅加载存储在该div中的值。这是一种使用PHP和jQuery实现酷炫且易于使用的Ajax/数据驱动应用程序的绝佳方式。不要把它变成一个jQuery线程,但这是最简单的方法。

0

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

function countdownload($filename) {

    if (file_exists($filename)) {

        file_put_contents($filename, 0);

    } else {

        file_put_contents($filename, file_get_contents($filename) + 1);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接