Html 网站上文件的最后修改日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1034794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Last Modified Date of a file on a web site
提问by Gerhard Weiss
Is there a way to get the Last-Modified-Date of a file on a Web Site?
有没有办法在网站上获取文件的上次修改日期?
i.e. Here is an example file I have out there: http://www.ymcadetroit.org/atf/cf/%7B2101903E-A11A-4532-A64D-9D823368A605%7D/Birmingham_Youth_Sports_Parent_Manual.pdf
即这是我在那里的示例文件:http: //www.ymcadetroit.org/atf/cf/%7B2101903E-A11A-4532-A64D-9D823368A605%7D/Birmingham_Youth_Sports_Parent_Manual.pdf
回答by Robert Borgersen
Go to the website you want to know about, wait for it to fully load, then go to the address bar and write this:
去你想了解的网站,等待它完全加载,然后去地址栏写下:
javascript:alert(document.lastModified)
You'll get a popup that says when it was last modified.
您将看到一个弹出窗口,显示上次修改时间。
回答by Gumbo
The HTTP intends the Last-Modified
header fieldto declare the last modification date. But the server needs to know that date.
HTTP 打算在Last-Modified
标头字段中声明最后修改日期。但是服务器需要知道那个日期。
On static files whose content is sent directly to the client and not interpreted otherwise by the server (e.g. .html
, .css
, .js
) it uses the last modified date of that file. But on files that generated content dynamically (PHP, Python, etc.) the script needs to specify that information itself. But unfortunatly many scripts don't to that.
在其内容直接发送到客户端且未被服务器以其他方式解释的静态文件上(例如.html
, .css
, .js
),它使用该文件的最后修改日期。但是在动态生成内容的文件(PHP、Python 等)上,脚本需要自己指定该信息。但不幸的是,许多脚本并没有做到这一点。
So if a Last-Modified
header field is present, you can use that information. But if not, you cannot determin the last modification date.
因此,如果存在Last-Modified
标头字段,则可以使用该信息。但如果不是,则无法确定最后修改日期。
回答by DRVic
I realize this question is 4 years old, but a search of the web proved that satisfactory answers remain rare. Peter's answer is part of the solution. When I had the same problem to solve, that got me started. But the rest of the solution...
我意识到这个问题已经有 4 年历史了,但在网上搜索证明满意的答案仍然很少见。彼得的回答是解决方案的一部分。当我有同样的问题需要解决时,这让我开始了。但其余的解决方案......
As he said, the web server must be configured to send the last-modified date ... so how do you configure the web server?
正如他所说,必须配置Web服务器才能发送最后修改日期......那么您如何配置Web服务器?
Assuming you have the necessary level of control, you first need to enable server side includes. There are several ways to do this - one of which is the "xbithack". A good reference is http://httpd.apache.org/docs/current/howto/ssi.html.
假设您拥有必要的控制级别,您首先需要启用服务器端包含。有几种方法可以做到这一点 - 其中之一是“xbithack”。一个很好的参考是http://httpd.apache.org/docs/current/howto/ssi.html。
Assuming you've done this, you need to set the execute bit on any html file that needs to have server-side includes parsed. This can be done at the command line of a UNIX-like system: chmod u+x file.html
or on the Mac using get-info (command-I) on the file.
假设您已完成此操作,您需要在任何需要解析服务器端包含的 html 文件上设置执行位。这可以在类 UNIX 系统的命令行中完成:chmod u+x file.html
或者在 Mac 上使用文件上的 get-info (command-I)。
This leaves the snippet to actually put in your file, which looks like this:
这使得代码片段实际放入您的文件中,如下所示:
This document last modified <!--#flastmod file="index.html" -->
This document last modified <!--#flastmod file="index.html" -->
Since I found many, many recommendations that didn't include this, and simply used the javascript document.lastModified
, I suspect that some servers give you what you want with the javascript version, whereas some (including the one hosting our stuff) don't.
由于我发现了许多不包含此内容的建议,并且只是使用了 javascript document.lastModified
,因此我怀疑有些服务器使用 javascript 版本为您提供了您想要的东西,而有些(包括托管我们的东西的服务器)则没有。
回答by Eddie Deyo
Here is some C# code to do it:
这是一些 C# 代码来做到这一点:
public DateTime GetLastModifyTime(string url)
{
WebRequest request = WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultNetworkCredentials;
request.Method = "HEAD";
using (WebResponse response = request.GetResponse())
{
string lastModifyString = response.Headers.Get("Last-Modified");
DateTime remoteTime;
if (DateTime.TryParse(lastModifyString, out remoteTime))
{
return remoteTime;
}
return DateTime.MinValue;
}
}
回答by Rakesh Kumar
To obtain the last modified date from client side, you can access the HTML DOM using the lastModified
property using JavaScript.
要从客户端获取上次修改日期,您可以lastModified
使用 JavaScript使用属性访问 HTML DOM 。
The lastModified
property grabs the information from the head portion sent with all web requests. The value can be manually set by developers on the web-server side of things so it may not reflect the actual last modified date of the file responsible for delivering the content.
该lastModified
属性从与所有 Web 请求一起发送的头部获取信息。该值可以由开发人员在 Web 服务器端手动设置,因此它可能不会反映负责交付内容的文件的实际最后修改日期。
Example
例子
<!DOCTYPE html>
<html>
<body>
<b>document.lastModified : </b>
<script>document.write( document.lastModified );</script>
</body>
</html>
The specific command in JavaScript that retrieves this is document.lastModified
and can easily be converted into a Date
object as follows :
JavaScript 中检索 this 的特定命令是document.lastModified
并且可以很容易地转换为一个Date
对象,如下所示:
var x = new Date(document.lastModified);
More information can be found on the site I used as a reference w3 schools : HTML DOM lastModified Property
更多信息可以在我用作参考w3 学校的站点上找到:HTML DOM lastModified Property
回答by Peter
I believe the web server must be configured to send the last-modified date in an HTTP-header, this is certainly one way. Check out section 14.29 Last-Modified of this document:
我相信必须将 Web 服务器配置为在 HTTP 标头中发送最后修改日期,这当然是一种方式。查看本文档的第 14.29 节 Last-Modified:
回答by jgallant
With just plain HTML, no you cannot.
仅使用纯 HTML,不,您不能。
You can with PHP, or ASP, or any other server side language.
您可以使用 PHP、ASP 或任何其他服务器端语言。
回答by DavidTaubmann
I'm not an expert in headers, but believe you are looking for this:
我不是标题方面的专家,但相信您正在寻找这个:
There is a way to check the date when a file was modified: View HTTP headers in Google Chrome?
有一种方法可以检查文件被修改的日期: 在 Google Chrome 中查看 HTTP 标头?
Check in there (Chrome's Developer Tools / Network / Selected File / Headers) the "If-Modified-Since" variable.
检查那里(Chrome 的开发人员工具/网络/选定文件/标题)“If-Modified-Since”变量。
Until now this has helped me to achieve what you are asking, get a file's modification date.
到目前为止,这已经帮助我实现了您的要求,获取文件的修改日期。
回答by Adrian Silveanu
You can do the following to get Last-Modified: https://superuser.com/a/991895
您可以执行以下操作以获得 Last-Modified:https: //superuser.com/a/991895
Using curl:
使用卷曲:
curl -s -v -X HEAD http://foo.com/bar/baz.pdf 2>&1 | grep '^< Last-Modified:'
Using wget:
使用 wget:
wget --server-response --spider http://example.com/bar/example.pdf 2>&1 | grep -i Last-Modified
回答by Igor Tarasov
In php:
在 php 中:
print getlastmod();
print gmdate('D, d M Y H:i:s', getlastmod());