简体   繁体   English

如何从用户上次通过.txt上传php时保存我的$ date

[英]How to save my $date from the last time the user uploaded php via .txt

I want it so that when my users upload a document I can present on the page the time the document was uploaded and therefore if they were to refresh the page the time the document was uploaded would still be there. 我希望这样做,以便当我的用户上传文档时,我可以在页面上显示文档上传的时间,因此,如果他们要刷新页面,则文档上传的时间仍然存在。 I have a document to store the time with which is updateLog.txt and is empty at the moment. 我有一个文档来存储时间,该时间是updateLog.txt,目前为空。 This is what I've currently got: 这是我目前所拥有的:

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]."\r\n");
fclose($myFile);

But nothing is saving to the document? 但是什么都没有保存到文档中?

You used $_POST[$date] instead of $date and a wrong time zone name. 您使用$ _POST [$ date]而非$ date和错误的时区名称。 The corresponding time zone would be Europe/London . 相应的时区将是Europe/London You can find a full list of time zones here: http://php.net/manual/en/timezones.php 您可以在此处找到时区的完整列表: http : //php.net/manual/en/timezones.php

Your code should look like this: 您的代码应如下所示:

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."\r\n");
fclose($myFile);

If you want to append to the file, use a as mode insted of w for fopen : 如果您要附加到文件时,用a作为模式insted的的wfopen

$myFile = fopen("uploadLog.txt","a") or exit("Can’t open file!");

File opening mode a+ , Open a file for read/write . 文件打开模式a+ ,打开文件进行read/write

The existing data in file is preserved. 文件中的现有数据将保留。 File pointer starts at the end of the file. 文件指针从文件末尾开始。 Creates a new file if the file doesn't exist. 如果文件不存在,则创建一个新文件。

a is use to write something to the file, w also do the same thing, but the difference is if we open a file as a mode then the data are stores like stack, if we open a file as w mode then it will Erases the contents of the file or creates a new file if it doesn't exist. a是用来写东西的文件, w也做同样的事情,但不同的是,如果我们打开一个文件作为a模式,然后将数据就像堆栈存储,如果我们打开一个文件w模式,那么它会擦除文件内容或创建一个新文件(如果不存在)。 File pointer starts at the beginning of the file. 文件指针始于文件的开头。

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."\r\n");


//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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM