php 如何下载一个php文件而不执行它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6321307/
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
How to download a php file without executing it?
提问by Jagadeesan
im working on a content management system for that i have to download a php file using php code without executing. any one can help me on this
我正在开发一个内容管理系统,我必须使用 php 代码下载一个 php 文件而不执行。任何人都可以帮助我
it is some thing like ftp. i have added the options to upload, edit and download a file. it is working fine. but while downloading a php file it is executed instead of downloading...
它有点像ftp。我添加了上传、编辑和下载文件的选项。它工作正常。但是在下载 php 文件时,它会执行而不是下载...
What i tried is:
我试过的是:
<?php
$file = $_REQUEST['file_name'];
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
include_once($file);
exit;
}
?>
回答by rzetterberg
You have to load the files content, write the content to the request and set the headers so that it's parsed as force download or octet stream.
您必须加载文件内容,将内容写入请求并设置标头,以便将其解析为强制下载或八位字节流。
For example:
例如:
http://server.com/download.php?name=test.php
http://server.com/download.php?name=test.php
Contents of download.php:
download.php 的内容:
<?php
$filename = $_GET["name"]; //Obviously needs validation
ob_end_clean();
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". filesize($filename).";");
header("Content-disposition: attachment; filename=" . $filename);
readfile($filename);
die();
?>
This code works without any modification. Although it needs validation and some security features.
此代码无需任何修改即可工作。虽然它需要验证和一些安全功能。
回答by Anders Abel
The server somehow identifies file that should be executed instead of downloaded. You have to exclude the .php file you want to download from that handling. The easiest is probably to rename the file to .php.txt.
服务器以某种方式标识应该执行而不是下载的文件。您必须从该处理中排除要下载的 .php 文件。最简单的方法可能是将文件重命名为 .php.txt。
Otherwise you should be able to configure the server to not process that particular file, or the path were it is located. How you do that depends on which server you are running.
否则,您应该能够将服务器配置为不处理该特定文件,或者它所在的路径。您如何做到这一点取决于您正在运行的服务器。
回答by LazyOne
If such php file is located on the same server/website, then just open it as normal file, e.g. $fileContents = file_get_contents($filename);
如果此类 php 文件位于同一服务器/网站上,则只需将其作为普通文件打开即可,例如 $fileContents = file_get_contents($filename);
If file is on another server, you have few possibleoptions:
如果文件在另一台服务器上,您有几个可能的选择:
1) Access it via FTP (if you have login details and access)
1)通过FTP访问(如果您有登录详细信息和访问权限)
2) Have special URL Rewrite rule on that server which will instruct web server to send file as plain text instead of executing it (e.g. somefile.php.txt
)
2)在该服务器上具有特殊的 URL 重写规则,该规则将指示 Web 服务器以纯文本形式发送文件而不是执行它(例如somefile.php.txt
)
3) Have special script on that server and by passing file name as a parameter it will return content of that file (e.g. http://example.com/showfile.php?file=somefile.php
)
3)在该服务器上有特殊的脚本,通过将文件名作为参数传递,它将返回该文件的内容(例如http://example.com/showfile.php?file=somefile.php
)
回答by Raymond
This is how to download a php file instead of executing it.
这是下载 php 文件而不是执行它的方法。
Trust me it works! ..download the file php with own risk :)
相信我它有效!..下载文件php,风险自负:)
<?php
function downloadThatPhp($nameOfTheFile)
{
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/text/x-vCard");
header("Content-Disposition: attachment; filename=".basename($nameOfTheFile).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($nameOfTheFile));
@readfile($nameOfTheFile);
exit(0);
}
// and this how to use:
// download that php file with your own risk :)
$file = $_REQUEST['file_name'];
$downloadThis = "http://domain-name.com/".$file;
if (file_exists($file)) {
downloadThatPhp($downloadThis);
}
?>
Hope this helps you bro :)
希望这对你有帮助兄弟:)
回答by ashkan nasirzadeh
if someone is looking to do this in his/her .htaccess file:
如果有人希望在他/她的 .htaccess 文件中执行此操作:
Header set Content-Disposition attachment
AddType application/octet-stream .php
or
或者
<FilesMatch "\.(?i:php)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
回答by Jan Dragsbaek
You can read alot about it on php.net/header, but to force a download, you can use a force-download header. This comment is amazing, check it out! :-)
您可以在php.net/header上阅读很多关于它的信息,但要强制下载,您可以使用 force-download 标头。这个评论太棒了,看看!:-)