PHP从txt文件运行代码 [英] PHP run code from txt file

查看:38
本文介绍了PHP从txt文件运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的网络服务器上有一个包含完整 PHP 代码且格式正确的文本文件,我是否可以使用 PHP fopen 读取文本文件并将 PHP 的输出回显到浏览器.IE.运行保存在文本文件中而不是硬编码的 PHP 代码?

If i had a text file on my web server, which contains full PHP code, all properly formatted, could i use PHP fopen to read the text file and echo the output of the PHP to the browser. Ie. Run PHP code that is held in a text file rather than hard coded?

谢谢

推荐答案

完全有可能做到这一点(毕竟它只是另一个文件),尽管我很想直接包含它而不是搞乱 fopen/eval等(注意:该文件当然必须完全形成"并以<?php"等开头,以便包含工作.)

It's perfectly possible to do this (it's just another file after all), although I'd be tempted to directly include it rather that messing around with fopen/eval, etc. (N.B.: The file would of course have to be "fully formed" and begin with "<?php", etc. for the include to work.)

但是,我会非常谨慎地使用 .txt 扩展名命名文件,因为这意味着可以直接从浏览器浏览该文件的内容,如果它存在于公共 HTTP 文档区域.因此,为什么不简单地将数据写入 .php 文件 - 这不会比 .txt 文件更困难,并且提供的优势是,如果有人试图访问它,它总是由网络服务器解析.

However, I'd be very wary of naming the file with a .txt extension as this will mean that it'll be possible to browse the contents of this file directly from the browser if it exists in the public HTTP docs area. As such, why not simply write the data into a .php file - this will be no more difficult than a .txt file and offers the advantage that it always be parsed by the web server if someone attempts to access it.

这篇关于PHP从txt文件运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆