doutuoben6908 2012-10-11 10:02
浏览 81
已采纳

如何在不执行它们的情况下允许将PHP文件上载到服务器

i want to make users to be able to upload PHP files and download them without executing them, i need to turn off PHP for a specific directory so it behaves as plain text.

i tried this but is it a proper solution ?

RemoveHandler .php .phtml .php3
RemoveType .php .phtml .php3
php_flag engine off
  • 写回答

1条回答 默认 最新

  • dph23577 2012-10-11 10:09
    关注

    You can use a Proxy Script to handle this.

    In your .htaccess file, you can redirect all the requests in the folder to change to this way:

    http://example.com/uploads/myfilewithadminaccess.php
    

    To

    http://example.com/uploads/index.php?file=myfilewithadminaccess.php
    

    Source of .htaccess

    RewriteEngine On
    RewriteRule ^([^/]*)$ ./index.php?file=$1 [L]
    

    And in the index.php just parse the file and give the output.

    Source of index.php:

    <?php
        header("Content-type: text/html");
        $filename = (file_exists("uploads/" . $_GET["file"])) ? "uploads/" . $_GET["file"] : "error.txt";
        $filecont = file_get_contents($filename);
        echo htmlspecialchars($filecont);
    ?>
    

    Fiddle: http://codepad.viper-7.com/68FSIU

    Note: You need to sanitize the inputs before you allow URLs to pass. So, people might inject ../, etc. those should be taken care.

    Hope this helps and it is perfectly fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?