SSI - 测试文件是否存在

发布于 2024-10-20 21:29:18 字数 198 浏览 2 评论 0原文

我正在根据变量动态添加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

沩ん囻菔务 2024-10-27 21:29:18

不——我害怕这个答案。但对于将来可能遇到这个问题的人来说,我确实找到了解决简单情况的方法。您可以编辑错误消息,在我的例子中,输出图像:

<!--#config errmsg="<img src='/file/testthisfile.jpg' alt='' />" -->

因此,如果文件不存在,您可以设置默认值。

必须强调,这仅适用于简单的情况,但这是一个很好的小解决方法!

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:

<!--#config errmsg="<img src='/file/testthisfile.jpg' alt='' />" -->

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!

紫南 2024-10-27 21:29:18

实际上与这里的答案相反,SSI 实际上支持文件存在测试。这是语法

<!--#if expr="-A /private" -->
  Click <a href="/private">here</a> to access private information.
<!--#endif -->

可能需要在您的 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

<!--#if expr="-A /private" -->
  Click <a href="/private">here</a> to access private information.
<!--#endif -->

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.

×眷恋的温暖 2024-10-27 21:29:18

SSI 不支持文件检测。

SSI does not support file detection.

戏蝶舞 2024-10-27 21:29:18

我想了一会儿,确实,ahgood 是对的,SSI 没有内置的文件检测功能,所以流量控制是有限的。

顺便说一句,我确实找到了对 SSI 扩展版本(基于 VMS 的系统)的引用

http://wasd.vsm.com.au/doc/env/env_0400.html

并且有一些扩展允许您以某种方式检查文件是否存在。

然而,更常见的情况是,如果使用 SSI,则可能会在 LAMP 环境中运行,因此可以利用 SSI 在 include 语句中运行 CGI/PHP 脚本的能力。

没有太多麻烦,人们可以求助于:

<body> 
<!--#include virtual="insert_intro.html" -->
<h2>Insert An Existing File</h2>
<!--#include 
virtual='checkFileExists.php?fn=insert_help.html&df=insert_default.html' -->
<h2>Insert a Non-Existing File</h2>
<!--#include 
virtual='checkFileExists.php?fn=insert_no_help.html&df=insert_default.html' -->
</body>

它使用 PHP 脚本来执行所有文件检查:

<?php
$theFileName = $_GET['fn'];
$theDefault  = $_GET['df'];
if ( file_exists($theFileName) === TRUE ) {
    include($theFileName);
} else {
    include($theDefault);
}
?>

我传递两个文件名,目标文件和备份/默认文件,脚本检查第一个文件,如果没有找到它,使用第二个。

这种方法引出了一个问题:当 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:

<body> 
<!--#include virtual="insert_intro.html" -->
<h2>Insert An Existing File</h2>
<!--#include 
virtual='checkFileExists.php?fn=insert_help.html&df=insert_default.html' -->
<h2>Insert a Non-Existing File</h2>
<!--#include 
virtual='checkFileExists.php?fn=insert_no_help.html&df=insert_default.html' -->
</body>

which uses a PHP script to do all the file checking:

<?php
$theFileName = $_GET['fn'];
$theDefault  = $_GET['df'];
if ( file_exists($theFileName) === TRUE ) {
    include($theFileName);
} else {
    include($theDefault);
}
?>

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).

时间海 2024-10-27 21:29:18

假设您正在运行 Apache 2.4,您可以使用 -F 选项(注意引用)。

<!--#if expr='-F "/private"' -->
  Click <a href="/private">here</a> to access private information.
<!--#endif -->

从文档 (http://httpd.apache.org/docs/current/expr. html):

如果字符串是一个有效的文件,可以通过所有服务器访问,则为真
该路径当前配置的访问控制。这使用了一个
内部子请求进行检查,因此请小心使用它 - 它可以
影响服务器的性能!

为了使示例正常工作,Apache 用户将需要访问您正在测试的目录/标志。您可能还需要 .htaccess 或 httpd.conf 文件中包含以下内容:

<Directory /private>
Require all granted
</Directory>

Assuming you are running Apache 2.4 you can use the -F option (note the quoting).

<!--#if expr='-F "/private"' -->
  Click <a href="/private">here</a> to access private information.
<!--#endif -->

From the docs (http://httpd.apache.org/docs/current/expr.html):

True if string is a valid file, accessible via all the server's
currently-configured access controls for that path. This uses an
internal subrequest to do the check, so use it with care - it can
impact your server's performance!

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:

<Directory /private>
Require all granted
</Directory>
久隐师 2024-10-27 21:29:18

您可以这样做,如下所示:

<!--#include virtual="/file/testthisfile.ssi" onerror="/file/default.ssi" -->

请注意,“-F”一元运算符以及“-A”一元运算符仅指路径可访问性,而不指资源的实际存在。

看看这里: http://httpd.apache.org/docs/2.4/expr .html(一元运算符)。

执行此类任务的运算符(-e、-s、-f)在 mod_include 下不可用。

You can do it, like this:

<!--#include virtual="/file/testthisfile.ssi" onerror="/file/default.ssi" -->

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.

诠释孤独 2024-10-27 21:29:18

正如其他人所说,SSI 没有文件检测功能,但有一个解决方法。

在服务器配置或 .htaccess 中

RewriteCond    %{DOCUMENT_ROOT}/some-dir/some-file.html -f
RewriteRule . - [NS,env=FILE_EXISTS:1]

注意:
NS = 不在 SSI 子请求中
env= 设置一个变量

然后在你的 html 中

<!--#if expr='v("FILE_EXISTS")'-->
  <!--#include virtual="/some-file/some-dir.html" -->
<!--#else -->
  It's not there
<!--#endif -->

注意:
在 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

RewriteCond    %{DOCUMENT_ROOT}/some-dir/some-file.html -f
RewriteRule . - [NS,env=FILE_EXISTS:1]

Note:
NS = not in SSI subrequests
env= set a variable

Then in your html

<!--#if expr='v("FILE_EXISTS")'-->
  <!--#include virtual="/some-file/some-dir.html" -->
<!--#else -->
  It's not there
<!--#endif -->

Note:
In the SSI, if you've redirected the url, variables get prefixed REDIRECT_{name}.
Use <pre><!--#printenv --></pre> to see what's happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文