-
Notifications
You must be signed in to change notification settings - Fork 0
/
working-cron-test.php
91 lines (77 loc) · 2.47 KB
/
working-cron-test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
// Tested in PHP7.0.18
$trackErrors = ini_get('track_errors');
ini_set('track_errors', 1);
$filename="test.txt";
if ( ! file_exists($filename) ) {
touch($filename);
}
$mode="r+";
$fh=fopen($filename, $mode);
if ( ! $fh ) {
echo "<LI style='color:red'>Failed to open $filename in $mode";
die();
}
if ( 1==1 ) {
flock($fh, LOCK_EX);
if ( 1==1 ) {
if (filesize($filename) > 0) {
$txt = fread($fh, filesize($filename));
echo "BEFORE:<OL>$txt</OL>";
}
else {
$txt = "empty";
}
$txt = "<LI>Date is now " . gmdate("Y-m-d H:i:s", time());
echo "<HR>WRITING:<OL>";
if ( 1==1 ) {
$overwrite = false;
if ($overwrite) {
echo "(Overwrite)<BR><BR>";
ftruncate($fh, 0);
// ftruncate is here as rewind will move the pointer
// to the beginning of the file and allow overwrite,
// but it will not remove existing content that
// extends beyond the length of the new write
rewind($fh);
}
else {
echo "(Appending)<BR><BR>";
}
if (fwrite($fh, $txt) == FALSE) {
echo "<LI style='color:red'>Failed to write to $filename";
die();
}
else {
echo "$txt";
}
fflush($fh);
}
echo "</OL>";
}
flock($fh, LOCK_UN);
}
fclose($fh);
// -------------------------------------------------------------------
echo "<HR>AFTER:<OL>";
if ( 1==2 ) {
// I've noticed that this block fails to pick up the newly
// written content when run with fread, but does work with
// the fgets logic below - possibly there is caching going on.
$mode = "r";
$fh2 = fopen($filename, $mode);
$contents = fread($fh2, filesize($filename));
echo "$contents";
fclose($fh2);
}
else {
$fh3 = fopen($filename, "r");
while (!feof($fh3)) {
$line = fgets($fh3);
echo $line;
}
fclose($fh3);
}
echo "</OL>";
echo "<HR>Fin";
?>