file_get_contents或文件函数挂起

I have developed an application for dental offices which sends automatic email and sms reminders at preset time of the day. This will be a local php application installed on client's machine since the database is local. I want to make it a monthly subscription service. In order to implement the subscription logic, my idea is to keep a text file on my server which will have an expiration date. The local application will read the contents of the text file to make sure that the subscription is not expired.

Now the problem is, when I use file() or file_get_contents() function in my script to read the text file on my server (for example, www.abcde.com/expiration.txt), it keeps forever to read the text file. Now if I directly put the url of the text file in seperate browser window, it opens it right away. Once I do that, the php script works fine. Again after a while, same cycle starts.

  • Am I missing something here?
  • Is there a better way to achieve the subscription logic?

My php application is 'local' using wamp server.

If i understand what you say, you want to create a subscription system for those messages right ?

You can use a new table in your DB with 3 cols : id, client_id, sub_time

It look pretty easy to check if the client is still subscriber of the messages like this i think. You just have to compare sub_time and current time, and see if ( $sub_time + 3600*24* $nb_of_day_in_last_month ) >= $current_time, or something like that ... :D

So, the new method i have is this one, hope it will work fine for you

The file for exemple named : subscribes.txt (i used a ',' delimitor, you can change it as it fit to you)

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

And the little script

$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 

And when you want to add a subscribe you just have to run this

$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 

Tell me if it still hangs or something, i'll try to find other solutions :)