如何在不执行的情况下下载php文件?

Jag*_*san 14 php download

我在内容管理系统上工作,我必须使用PHP代码下载一个PHP文件而不执行.任何人都可以帮助我

它有点像ftp.我添加了上传,编辑和下载文件的选项.它工作正常.但在下载一个php文件时,它被执行而不是下载...

我尝试的是:

<?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;
}
?>
Run Code Online (Sandbox Code Playgroud)

rze*_*erg 22

您必须加载文件内容,将内容写入请求并设置标头,以便将其解析为强制下载或八位字节流.

例如:

http://server.com/download.php?name=test.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();
  ?>
Run Code Online (Sandbox Code Playgroud)

此代码无需任何修改即可运行.虽然它需要验证和一些安全功能.

  • 是的,这是100%的真实!然而,这就是OP所要求的. (2认同)