如何将上传的php文件显示为纯文本而不是在wordpress中执行?

前端之家收集整理的这篇文章主要介绍了如何将上传的php文件显示为纯文本而不是在wordpress中执行?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
编辑/ tmp中的testme.PHP文件.
  1. <?PHP
  2. echo "test";
  3. ?>

编辑一篇名为test and upload file /tmp/testme.PHP的新帖子,用url http://home.local/wp/?p = 4785发布.

我想看看testme中的内容,点击它,在wordpress中弹出新窗口.

继续点击它.网页上显示的测试结果.

我的期望:
1.只需在测试帖中点击testme一次.
2.将testme.PHP显示为纯文本,

  1. <?PHP
  2. echo "test";
  3. ?>

而不是执行testme.PHP的结果.

  1. test

我根据一些材料显示配置PHP文件作为apache中的纯文本.

sudo vim /etc/apache2/sites-available/000-default.conf

  1. <VirtualHost *:80>
  2. ServerName www.home.local
  3. ServerAdmin webmaster@localhost
  4. DocumentRoot /var/www/html
  5. ErrorLog ${APACHE_LOG_DIR}/error.log
  6. CustomLog ${APACHE_LOG_DIR}/access.log combined
  7. <Directory /var/www/html>
  8. Options Indexes FollowSymLinks MultiViews
  9. AllowOverride All
  10. allow from all
  11. PHP_flag engine off
  12. AddType text/plain PHP
  13. </Directory>
  14. </VirtualHost>

重启apache2(在debian中构建).

  1. sudo systemctl restart apache2

要打开帖子http://home.local/wp/?p = 4785,我在网页上得到以下输出

  1. <?PHP
  2. /**
  3. * Front to the wordpress application. This file doesn't do anything,but loads
  4. * wp-blog-header.PHP which does and tells wordpress to load the theme.
  5. *
  6. * @package wordpress
  7. */
  8.  
  9. /**
  10. * Tells wordpress to load the wordpress theme and output it.
  11. *
  12. * @var bool
  13. */
  14. define('WP_USE_THEMES',true);
  15.  
  16. /** Loads the wordpress Environment and Template */
  17. require( dirname( __FILE__ ) . '/wp-blog-header.PHP' );
您已经拥有了正确的代码 – AddType text / plain PHP,这将使Apache将PHP文件(或名称以.PHP结尾的文件)视为纯文本文件.

但假设如下:

>您在/ var / www / html目录中安装了wordpress.
> PHP文件上传wordpress中的默认上传文件夹(wp-content / uploads).

如果将目录设置为/ var / www / html / wp-content / uploads,如下所示:

  1. <Directory /var/www/html/wp-content/uploads>
  2. AddType text/plain PHP
  3. </Directory>

你会得到你想要的结果 – 只有uploads文件夹中的PHP文件将被视为纯文本文件,而不是/ var / www / html目录中的所有PHP文件.这解释了“要打开帖子http://home.local/wp/?p = 4785,我得到以下输出”的问题,其中输出文件/ var / www / html / index中的代码. PHP.我的意思是,你使用< Directory / var / www / html>,这使得Apache将index.PHP文件视为纯文本文件,而不是在文件中执行PHP代码.

备用方法:使用.htaccess文件.

特别是如果您无法编辑或无法访问Apache的配置(.conf)文件.

>在uploads文件夹中创建.htaccess,如果它还没有.
>然后在该文件添加AddType text / plain PHP.

补充说明

I want to see the content in testme.PHP,click it,pop up new window

我确信你可以做到这一点或者已经拥有代码/解决方案,但是一个简单的方法,就是将target =“_ blank”添加到附件/文件链接..或者使用JavaScript,你可以使用window.open().

并且您必须采取全面的安全措施,因为允许人们上传PHP文件可能会损害您的网站和/或您的网站用户.

猜你在找的PHP相关文章