SSI - 测试文件是否存在
我正在根据变量动态添加 ssi 包含,并且我希望能够有一个默认包含,以防文件不存在。即:
if /file/testthisfile.ssi exists
add /file/testthisfile.ssi
else
add /file/default.ssi
这可能吗?
谢谢!
I'm dynamically adding ssi includes based on variables and I would like to be able to have a default include in case a file doesn't exist. ie:
if /file/testthisfile.ssi exists
add /file/testthisfile.ssi
else
add /file/default.ssi
Is this possible?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不——我害怕这个答案。但对于将来可能遇到这个问题的人来说,我确实找到了解决简单情况的方法。您可以编辑错误消息,在我的例子中,输出图像:
因此,如果文件不存在,您可以设置默认值。
必须强调,这仅适用于简单的情况,但这是一个很好的小解决方法!
No - I was afraid of that answer. But for anyone who might come across this question in the future I did find a work around for simple cases. You can edit the error message and in my case, output an image:
So if the file doesn't exist you can set a default.
Must underline that this will only work for simple cases, but it's a nice little work around!
实际上与这里的答案相反,SSI 实际上支持文件存在测试。这是语法
可能需要在您的 apache 配置中启用对 -A 标志的支持。
SSI 中使用的表达式已被分解到此处记录的 apache 表达式模块中
http: //httpd.apache.org/docs/current/expr.html
但 -A 标志在“传统”SSI 表达式解析器中也可用。
Actually contrary to the answers here, SSI does in fact support file existence tests. this is the syntax
Support for the -A flag may need to be enabled in your apache configuration.
The expressions used in this spot of SSI have been factored out into an apache expressions module documented here
http://httpd.apache.org/docs/current/expr.html
but the -A flag is also available in "legacy" SSI expression parsers.
SSI 不支持文件检测。
SSI does not support file detection.
我想了一会儿,确实,ahgood 是对的,SSI 没有内置的文件检测功能,所以流量控制是有限的。
顺便说一句,我确实找到了对 SSI 扩展版本(基于 VMS 的系统)的引用
http://wasd.vsm.com.au/doc/env/env_0400.html
并且有一些扩展允许您以某种方式检查文件是否存在。
然而,更常见的情况是,如果使用 SSI,则可能会在 LAMP 环境中运行,因此可以利用 SSI 在 include 语句中运行 CGI/PHP 脚本的能力。
没有太多麻烦,人们可以求助于:
它使用 PHP 脚本来执行所有文件检查:
我传递两个文件名,目标文件和备份/默认文件,脚本检查第一个文件,如果没有找到它,使用第二个。
这种方法引出了一个问题:当 PHP 可用时为什么要使用 SSI?在某些情况下,特别是在遗留系统中,可能有一个基于 SSI 的大型网站,并且解决方法虽然不太优雅,但可以解决问题。
PHP 不是必需的,PERL 脚本也可以。
最后,我尝试使用 PHP 的 apache_setenv ,但我不知道如何在 PHP、Apache 和 SSI 之间传递环境变量(我也尝试设置 $_SERVER 和 $_ENV 变量,但没有成功) 。
I thought about this for a while, and indeed, ahgood was correct, SSI does not have a built-in file detection function, so flow control is limited.
As an aside, I did find a reference to an extended version of SSI (a VMS based system)
http://wasd.vsm.com.au/doc/env/env_0400.html
and there were some extensions that would allow you to check for file existence in some sort of a fashion.
However, more often then not, if one were using SSI, one would probably be running in a LAMP environment, so one could take advantage of SSI's ability to run a CGI/PHP script in the include statement.
Without too much trouble, one could resort to:
which uses a PHP script to do all the file checking:
I pass two file names, the intended file and the backup/default file, the script checks for the first and if it is not found, uses the second.
This approach begs the question, why use SSI when PHP is available? In some cases, especially in a legacy system, there may be a big website based on SSI and a work-around, though less elegant, would solve a problem.
PHP is not mandatory, a PERL script would also work.
Finally, I did experiment with trying to use PHP's
apache_setenv
but I could not figure out how to pass environment variables between PHP, Apache and SSI (I also tried setting $_SERVER and $_ENV variables but without success).假设您正在运行 Apache 2.4,您可以使用 -F 选项(注意引用)。
从文档 (http://httpd.apache.org/docs/current/expr. html):
为了使示例正常工作,Apache 用户将需要访问您正在测试的目录/标志。您可能还需要 .htaccess 或 httpd.conf 文件中包含以下内容:
Assuming you are running Apache 2.4 you can use the -F option (note the quoting).
From the docs (http://httpd.apache.org/docs/current/expr.html):
For the example to work the Apache user will need access to the direcotory/flag that you are testing. You may also need the following in a .htaccess or httpd.conf file:
您可以这样做,如下所示:
请注意,“-F”一元运算符以及“-A”一元运算符仅指路径可访问性,而不指资源的实际存在。
看看这里: http://httpd.apache.org/docs/2.4/expr .html(一元运算符)。
执行此类任务的运算符(-e、-s、-f)在 mod_include 下不可用。
You can do it, like this:
Please note that "-F" unary operator, as well as "-A" unary operator, only refer to path accessibility and not to actual existence of the resource.
Have a look here: http://httpd.apache.org/docs/2.4/expr.html (Unary operators).
Operators performing such task (-e, -s, -f) are not available under mod_include.
正如其他人所说,SSI 没有文件检测功能,但有一个解决方法。
在服务器配置或 .htaccess 中
注意:
NS = 不在 SSI 子请求中
env= 设置一个变量
然后在你的 html 中
注意:
在 SSI 中,如果您重定向了 URL,则变量的前缀为
REDIRECT_{name}
。使用
查看发生了什么。
As others have said, SSI doesn't have file detection, but there is a work around.
In server config or .htaccess
Note:
NS = not in SSI subrequests
env= set a variable
Then in your html
Note:
In the SSI, if you've redirected the url, variables get prefixed
REDIRECT_{name}
.Use
<pre><!--#printenv --></pre>
to see what's happening.