php file_get_contents 创建的文件不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3265922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
file_get_contents create file is not exists
提问by ParoX
Is there any alternative to file_get_contents that would create the file if it did not exist. I am basically looking for a one line command. I am using it to count download stats for a program. I use this PHP code in the pre-download page:
如果 file_get_contents 不存在,是否有任何替代方法可以创建该文件。我基本上是在寻找一个单行命令。我正在使用它来计算程序的下载统计信息。我在预下载页面中使用了这个 PHP 代码:
Download #: <?php $hits = file_get_contents("downloads.txt"); echo $hits; ?>
and then in the download page, I have this.
然后在下载页面,我有这个。
<?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.'" />';
?>
Naturally though, the user accesses the pre-download page first, so its not created yet. I do not want to add any functions to the pre download page, i want it to be plain and simple and not alot of adding/changing.
当然,用户首先访问预下载页面,因此尚未创建。我不想向预下载页面添加任何功能,我希望它简单明了,不要添加/更改很多。
Edit:
编辑:
Something like this would work, but its not working for me?
像这样的东西会起作用,但对我不起作用?
$count = (file_exists($filename))? file_get_contents($filename) : 0; echo $count;
回答by Sergey Eremin
Download #: <?php
$hits = '';
$filename = "downloads.txt";
if (file_exists($filename)) {
$hits = file_get_contents($filename);
} else {
file_put_contents($filename, '');
}
echo $hits;
?>
you can also use fopen()with 'w+' mode:
您还可以使用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;
?>
回答by hyperlexic
Type juggling like this can lead to crazy, unforeseen problems later. to turn a string to an integer, you can just add the integer 0 to any string.
像这样打字杂耍可能会导致以后出现疯狂的、无法预料的问题。要将字符串转换为整数,您可以将整数 0 添加到任何字符串。
For example:
例如:
$f = file_get_contents('file.php');
$f = $f + 0;
echo is_int($f); //will return 1 for true
however, i second the use of a database instead of a text file for this. there's a few ways to go about it. one way is to insert a unique string into a table called 'download_count' every time someone downloads the file. the query is as easy as "insert into download_count $randomValue" - make sure the index is unique. then, just count the number of rows in this table when you need the count. the number of rows is the download count. and you have a real integer instead of a string pretending to be an integer. or make a field in your 'download file' table that has a download count integer. each file should be in a database with an id anyway. when someone downloads the file, pull that number from the database in your download function, put it into a variable, increment, update table and show it on the client however you want. use PHP with jQuery Ajax to update it asynchronously to make it cool.
但是,我建议为此使用数据库而不是文本文件。有几种方法可以解决这个问题。一种方法是在每次有人下载文件时将一个唯一的字符串插入一个名为“download_count”的表中。查询就像“插入 download_count $randomValue”一样简单 - 确保索引是唯一的。然后,只需在需要计数时计算此表中的行数。行数是下载计数。你有一个真正的整数而不是一个假装是整数的字符串。或者在“下载文件”表中创建一个具有下载计数整数的字段。无论如何,每个文件都应该在一个带有 id 的数据库中。当有人下载文件时,从下载函数中的数据库中提取该数字,将其放入一个变量中,递增,更新表并根据需要在客户端上显示它。使用 PHP 和 jQuery Ajax 异步更新它以使其更酷。
i would still use php and jquery.load(file.php) if you insist on using a text file. that way, you can use your text file for storing any kind of data and just load the specific part of the text file using context selectors. the file.php accepts the $_GET request, loads the right portion of the file and reads the number stored in the file. it then increments the number stored in the file, updates the file and sends data back to the client to be displayed any way you want. for example, you can have a div in your text file with an id set to 'downloadcount' and a div with an id for any other data you want to store in this file. when you load file.php, you just send div#download_count along with the filename and it will only load the value stored in that div. this is a killer way to use php and jquery for cool and easy Ajax/data driven apps. not to turn this into a jquery thread, but this is as simple as it gets.
如果您坚持使用文本文件,我仍然会使用 php 和 jquery.load(file.php)。这样,您可以使用文本文件来存储任何类型的数据,只需使用上下文选择器加载文本文件的特定部分。file.php 接受 $_GET 请求,加载文件的正确部分并读取文件中存储的数字。然后它增加存储在文件中的数字,更新文件并将数据发送回客户端以按您想要的任何方式显示。例如,您可以在文本文件中有一个 div,其 id 设置为“downloadcount”,而 div 的 id 则表示您想要存储在此文件中的任何其他数据。当您加载 file.php 时,您只需将 div#download_count 与文件名一起发送,它只会加载存储在该 div 中的值。这是将 php 和 jquery 用于酷炫而简单的 Ajax/数据驱动应用程序的杀手级方式。
回答by simhumileco
You can use more concise equivalent yours function countdownload:
您可以使用更简洁的等效函数countdownload:
function countdownload($filename) {
if (file_exists($filename)) {
file_put_contents($filename, 0);
} else {
file_put_contents($filename, file_get_contents($filename) + 1);
}
}

