我希望它,以便当我的用户上传文档时,我可以在页面上显示文档的上传时间,因此如果他们要刷新页面,文档上传的时间仍然存在。我有一个文档来存储更新日志的时间.txt目前是空的。这是我目前得到的:
date_default_timezone_set('United Kingdome/Londone');
$date = date('d/m/Y h:i:s a', time());
$myFile=fopen("uploadLog.txt","w") or exit("Can’t open file!");
fwrite($myFile, $_POST[$date]."rn");
fclose($myFile);
但是没有保存到文档中吗?
$_POST[$date] 而不是 $date 和错误的时区名称。相应的时区将是 Europe/London
.您可以在此处找到时区的完整列表:http://php.net/manual/en/timezones.php
您的代码应如下所示:
date_default_timezone_set('Europe/London');
$date = date('d/m/Y h:i:s a', time());
$myFile = fopen("uploadLog.txt","w") or exit("Can’t open file!");
fwrite($myFile, $date."rn");
fclose($myFile);
如果要追加到文件,请使用a
as 模式而不是 w
for fopen
:
$myFile = fopen("uploadLog.txt","a") or exit("Can’t open file!");
文件打开模式a+
,打开文件进行read/write
。
将保留文件中的现有数据。文件指针从文件末尾开始。如果文件不存在,则创建新文件。
a
用于向文件写入某些内容,w
也做同样的事情,但不同之处在于,如果我们以a
模式打开文件,那么数据就像堆栈一样存储,如果我们以w
模式打开文件,那么它将删除文件的内容或创建一个新文件(如果它不存在)。文件指针从文件的开头开始。
date_default_timezone_set('Europe/London');
$date = date('d/m/Y h:i:s a', time());
$myFile=fopen("uploadLog.txt","a+") or exit("Can’t open file!");
fwrite($myFile, $date."rn");
//If you want to read the file, then Output one line until end-of-file
while(!feof($myFile)) {
echo fgets($myFile) . "<br>"; //data pull to the php
}
fclose($myFile);