チェ・ゲバムラの日記

脱犬の道を目指す男のブログ

PHPで外部ファイルから読み込んで保存

問い合わせフォームを作ってて、

受付番号をつけることになった。

仕様は 日付+0001 ってなって連番にする。

日付が変わったらまたカウント1からスタートする。

 

実はWordpressのコンタクトフォームの話で、mail.phpに書いた内容。

すげーみづらいかもだがこれで動いた。

 

あと見ればわかるけど

temp.txt に1とかいれておいてアップ。

tempdate.txt に本日の日付いれたり昨日の日付いれたりしてテストしてみた。

 

$nowdate = date_i18n( Ymd, $timestamp );

setlocale(LC_ALL, 'ja_JP.UTF-8');


//前に送信された日付をファイルから読み込み
$tempdate_data = file_get_contents('wp/wp-content/plugins/contact-form-7/includes/tempdate.txt');

 

//今の日付と前の日付を比較
//一致してたらtempファイルの数字を1進めてファイルに書き込み
if($nowdate == $tempdate_data){
//tempファイルを読み込んで変数に格納
$temp_cnt = file_get_contents('wp/wp-content/plugins/contact-form-7/includes/temp.txt');
$temp_cnt++;
file_put_contents("wp/wp-content/plugins/contact-form-7/includes/temp.txt",$temp_cnt);
}
//前の日付と違うなら今の日付をファイルに書き込む
else{
file_put_contents("wp/wp-content/plugins/contact-form-7/includes/tempdate.txt", $nowdate);
//カウントも1にリセット
$temp_cnt = 1;
file_put_contents("wp/wp-content/plugins/contact-form-7/includes/temp.txt", $temp_cnt);
}

 

$temp_cnt = sprintf("%04d", $temp_cnt);
$tempdate_data = file_get_contents('wp/wp-content/plugins/contact-form-7/includes/tempdate.txt');
return $nowdate . $temp_cnt;

 

 

sprintf("%04d", $temp_cnt); これで桁数をフォーマットできる 便利