【问题标题】:file_get_contents or file function hangsfile_get_contents 或文件功能挂起
【发布时间】:2014-07-03 01:38:57
【问题描述】:

我为牙科诊所开发了一个应用程序,它可以在一天中的预设时间发送自动电子邮件和短信提醒。这将是安装在客户端机器上的本地 php 应用程序,因为数据库是本地的。我想让它成为每月订阅服务。为了实现订阅逻辑,我的想法是在我的服务器上保留一个具有到期日期的文本文件。本地应用程序将读取文本文件的内容以确保订阅未过期。

现在的问题是,当我在脚本中使用 file() 或 file_get_contents() 函数来读取服务器上的文本文件(例如 www.abcde.com/expiration.txt)时,它会一直读取文本文件。现在,如果我直接将文本文件的 url 放在单独的浏览器窗口中,它会立即打开它。一旦我这样做了,php脚本就可以正常工作。过了一会儿,同样的循环又开始了。

  • 我在这里遗漏了什么吗?
  • 有没有更好的方法来实现订阅逻辑?

我的 php 应用程序是使用 wamp 服务器的“本地”应用程序。

【问题讨论】:

  • 我怀疑www.abcde.com/expiration.txt 是您服务器上有效的本地相对路径,您需要在方案前面加上:http://www.abcde.com/expiration.txt。请记住,您不能将内容放在远程网站上。为什么不使用数据库而不是文本文件?
  • 嗨。感谢您的答复。我不能使用客户数据库。我只从中获取数据并使用它来发送消息。远程网站是我自己公司的网站,将托管不同客户的许可证到期详细信息。

标签: php file-get-contents


【解决方案1】:

如果我明白你的意思,你想为这些消息创建订阅系统吗?

您可以在数据库中使用具有 3 个列的新表:id、client_id、sub_time

我认为检查客户端是否仍然是此类消息的订阅者看起来很容易。你只需要比较 sub_time 和当前时间,看看 ($sub_time + 3600*24* $nb_of_day_in_last_month ) >= $current_time,或者类似的东西......:D

【讨论】:

  • 感谢您的回复。我无法添加或编辑或修改数据库。它是客户的数据库。我只是从该数据库中获取姓名、手机、电子邮件地址等记录来发送消息。我没有使用我自己的任何类型的数据库。还有其他想法吗?
  • aahh 该死的,你仍然可以用一个文件来做到这一点,你可以像数据库一样使用它:) 我正在尽快用脚本发布一个新答案:p
  • 谢谢。非常感谢您的帮助。
【解决方案2】:

所以,我的新方法就是这个,希望它对你有用

示例文件名为:subscribes.txt(我使用了“,”分隔符,您可以根据需要更改它)

; a comment line
43,1404399704
75,1404406800
104,1404399200
6,1404399500

还有小脚本

$file = 'subscribes.txt';
$delim= ','; // set the delimitor of your cols

$time = time(); // get time to use it in all the script

// this part is to get the last month number of days
$y  = date('Y'); // year
$lm = date('n') - 1; // last month
$y  = ($lm == 0) ? $y - 1 : $y; // if we are in jan., year - 1
$lm = ($lm == 0) ? 12 : $lm; // if we are in jan., last month was dec.
$lm = mktime( 0, 0, 0, $lm, 1, $y ); // so, the last month
$lm_days = intval(date("t",$lm)); // get the number of days in last month

// you can get the current month number of days with :
// $lm_days = intval(date('t'));

// end of the part


$file = file_get_contents($file); // read file content

$file = explode(PHP_EOL, $file); // transform it in array

foreach( $file as $lk => $line ){

    if( !empty($line) && !preg_match('/^;(.*)/', $line) ){ // if line not empty and don't start with ;

        $cid= null;
        $st = null;

        list($cid,$st) = explode($delim, trim($line)); // explode the 3 cols of the line

        $st = $st + 3600*24*$lm_days;

        if( $time >= $st ){

            // send notifications etc etc ... (eg: your script)
        }
        else{

            // else we will delete the line to light the file
            unset($file[$lk]);
        }
    }
}

// once done, letz write the lighted file
$file = implode(PHP_EOL, $file); // convert array to string

file_put_contents($file, $file); // put it back in the file 

当你想添加订阅时,你只需要运行这个

$file_name = 'subscribes.txt';
$delim= ','; // set the delimitor of your cols

$time = time(); // get time to use it in all the script

$file = file_get_contents($file_name); // read file content

$file = explode(PHP_EOL, $file); // transform it in array

$file[] = $cid.$delim.time(); // add to array a new entry
// there you can add multiple entries, let it fit to you :P

$file = implode(PHP_EOL, $file); // convert array to string

file_put_contents($file, $file); // put it back in the file 

如果它仍然挂起,请告诉我,我会尝试寻找其他解决方案:)

【讨论】:

  • 衷心感谢您对我的帮助。我所做的是为每个包含到期日期的客户端创建一个单独的文本文件。例如,文本文件“license201938474.txt”用于客户端 A,其内容为 2016-01-31 到期日期。现在在我的 php 脚本中,我使用 file_get_contents('license201938474.txt') 读取文件并使用 strtotime() 函数获取时间戳。然后我将该时间戳与当前时间戳进行比较,以查看到期日期的时间戳是否大于当前时间戳。
  • $phonenumber = ''; $licenseData = ''; $expiryTimestamp = ''; $curtime = 时间(); $phonenumber = preg_replace("/[^0-9]/","",$prefs['Office-Phone']); $licenseData = file_get_contents('<a href="/default/index/tourl?u=aHR0cDovL3d3dy5hYmNkZS5jb20vbGljZW5zZScuJTI0cGhvbmVudW1iZXIuJy50eHQn" rel="nofollow" target="_blank">abcde.com/license'.$phonenumber.'.txt'</a>); $expiryTimestamp = strtotime($licenseData); echo "<br>Exp 时间戳:".$expiryTimestamp."<br>当前时间戳:".$curtime; $timediff = $expiryTimestamp - $curtime; echo "<br>时差:".$timediff;回声($timediff &gt; 0); if((expiryTimestamp - time()) window.location = 'error.php';";死(); }
  • np ^^ 我喜欢测试脚本 ^^ 我刚刚看到您从网络服务器获取文件,而不是从本地获取文件,您是否测试过您是否有权获取它? (如果您不在 ftp 中,我认为您不能将内容放在遥远的 url 上……)。如果文件与脚本在同一台服务器上,请尝试使用本地路径访问它..也许cURL:p
  • 感谢您的回复。我的 php 脚本是本地的,并通过 wamp 服务器在客户端机器上本地运行。许可证文本文件位于我公司托管空间的云中。因此,为了远程管理我在客户端机器上安装的 php 脚本的订阅,我将许可证文件放在了远程位置。
  • np,所以你应该通过 PHP 使用 FTP 来修改远程文件 ^^ 但是,我不太擅长那个,你应该在这里搜索,或者发布一个新问题 :) 我有时间我会探索这条路的:)
猜你喜欢
  • 2012-06-22
  • 1970-01-01
  • 2021-10-05
  • 2020-03-13
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 2015-07-14
  • 1970-01-01
相关资源
最近更新 更多