简体   繁体   English

我可以设置一个PHP文件作为文件扩展名的处理程序吗?

[英]Can I set up a PHP file to act as a handler for a file extension?

Lets say I have a file with a .quote extension. 可以说我有一个扩展名为.quote的文件。 I'd like the web server to send the contents of this file through a simple PHP script before sending it to the browser. 我希望Web服务器先通过简单的PHP脚本发送此文件的内容,然后再将其发送到浏览器。

For example: 例如:

This is a quote 这是一个报价

Would be transformed to 将转化为

"This is a quote" “这是一个报价”


It looks as though I need to put something in my .htaccess along the lines of 似乎我需要按照以下方式在.htaccess放入一些内容:

AddHandler x-quote .quote
Action x-quote /cgi/quote.cgi

And then my script would be something like 然后我的脚本会像

#!usr/local/php5
<?php
    header("Content-type: text/plain");

    #I don't know how to do this step...
    echo '"'.$CONTENTS_OF_THE_FILE.'"'; 
?>

However, now I just get an error 404 when I look at the .quote file on the server. 但是,现在当我查看服务器上的.quote文件时,只会出现错误404。 I can think of a few things that might be causing this: 我可以想到可能导致此问题的一些原因:

  1. The cgi file is not being found and/or executed - How can I determine if cgi files are allowed to run on my server? 没有找到和/或执行 cgi文件-如何确定是否允许在我的服务器上运行cgi文件? Should the path be relative to the webroot? 路径应该相对于webroot吗?
  2. The path to the php executable is incorrect - Is this listed somewhere in php.ini? php可执行文件的路径不正确 -这是否在php.ini中列出? If so, where? 如果是这样,在哪里?
  3. I'm not doing the right thing in the PHP itself - What should I be doing 我没有在PHP本身中做正确的事情 -我应该怎么做

I do not have SSH access to the server in question. 我没有对有问题的服务器的SSH访问。

Why not just run it through a normal PHP file rather than CGI and use mod_rewrite? 为什么不通过普通的PHP文件而不是CGI运行它并使用mod_rewrite? Your .htaccess would look like 您的.htaccess看起来像

RewriteRule ^(.+?).quote$ quote.php?f=$1

Your PHP: 您的PHP:

<?php

header("Content-type: text/plain");

$quote = file_get_contents($_GET['f']);
echo '"'.$quote.'"';   

?>

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

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