如何让人们下载.php文件而不解释代码?

时间:2021-11-27 01:13:26

I'm using apache2 and enabled a site that has no index.html file in its <Directory> directive, so an index of all the files is displayed instead of an html webpage.

我正在使用apache2并在其 指令中启用了一个没有index.html文件的站点,因此显示所有文件的索引而不是html网页。

I've included some .php files that I want to allow other people to download, however whenever I download a .php file from this page, it is already 'interpreted'.

我已经包含了一些我希望允许其他人下载的.php文件,但每当我从这个页面下载.php文件时,它都已被“解释”了。

For example if I save hello.php onto my desktop

例如,如果我将hello.php保存到我的桌面上

<? php $var = 'hello world'; echo $var ?>

When I open up hello.php the code reads

当我打开hello.php代码读取

hello

Is there any way I can allow people to download the code before it is interpreted?

有没有什么办法可以让人们在解释之前下载代码?

5 个解决方案

#1


3  

Depending on the server configuration you need a .htaccess with:

根据服务器配置,您需要.htaccess:

# for mod_php
php_value engine off

Or:

# for CGI setups
Options -ExecCGI

Or:

# for FastCGI this depends on the name of the handler script
RemoveHandler .php
#RemoveHandler php5-fastcgi .php

And sometimes even a brute-force method:

有时甚至是蛮力方法:

<FilesMatch "[.]php\d?$">
   ForceType text/php
   SetHandler default-handler
</FilesMatch>

#2


1  

This depends on the behavior at the server side. For example, if the mod_php is disabled in Apache, and the MIME type of ".php" is configured as "application/binary", then accessing to "http://[server_ip]/hello.php" will cause the php file to be downloaded to the client.

这取决于服务器端的行为。例如,如果在Apache中禁用了mod_php,并且MIME类型“.php”被配置为“application / binary”,则访问“http:// [server_ip] /hello.php”将导致php文件下载到客户端。

#3


0  

Remove the php5 module from the apache configuration.

从apache配置中删除php5模块。

#4


0  

You could probably use file_get_contents() to display the code contained within the file

您可以使用file_get_contents()来显示文件中包含的代码

http://php.net/manual/en/function.file-get-contents.php

#5


0  

You can try giving it a .phps extension, or a .txt extension. Some servers (if configured to do so) will allow the user to see a syntax highlighted version of the file if it has a .phps extension. Alternatively, you could use .htaccess to tell apache to not execute the file as php and force a download by telling it to send a application/octet-stream content type.

您可以尝试为其提供.phps扩展名或.txt扩展名。某些服务器(如果配置为这样做)将允许用户查看文件突出显示的文件版本(如果它具有.phps扩展名)。或者,您可以使用.htaccess告诉apache不要将文件作为php执行,并通过告诉它发送应用程序/八位字节流内容类型来强制下载。

#1


3  

Depending on the server configuration you need a .htaccess with:

根据服务器配置,您需要.htaccess:

# for mod_php
php_value engine off

Or:

# for CGI setups
Options -ExecCGI

Or:

# for FastCGI this depends on the name of the handler script
RemoveHandler .php
#RemoveHandler php5-fastcgi .php

And sometimes even a brute-force method:

有时甚至是蛮力方法:

<FilesMatch "[.]php\d?$">
   ForceType text/php
   SetHandler default-handler
</FilesMatch>

#2


1  

This depends on the behavior at the server side. For example, if the mod_php is disabled in Apache, and the MIME type of ".php" is configured as "application/binary", then accessing to "http://[server_ip]/hello.php" will cause the php file to be downloaded to the client.

这取决于服务器端的行为。例如,如果在Apache中禁用了mod_php,并且MIME类型“.php”被配置为“application / binary”,则访问“http:// [server_ip] /hello.php”将导致php文件下载到客户端。

#3


0  

Remove the php5 module from the apache configuration.

从apache配置中删除php5模块。

#4


0  

You could probably use file_get_contents() to display the code contained within the file

您可以使用file_get_contents()来显示文件中包含的代码

http://php.net/manual/en/function.file-get-contents.php

#5


0  

You can try giving it a .phps extension, or a .txt extension. Some servers (if configured to do so) will allow the user to see a syntax highlighted version of the file if it has a .phps extension. Alternatively, you could use .htaccess to tell apache to not execute the file as php and force a download by telling it to send a application/octet-stream content type.

您可以尝试为其提供.phps扩展名或.txt扩展名。某些服务器(如果配置为这样做)将允许用户查看文件突出显示的文件版本(如果它具有.phps扩展名)。或者,您可以使用.htaccess告诉apache不要将文件作为php执行,并通过告诉它发送应用程序/八位字节流内容类型来强制下载。