dongmeng1868 2009-02-06 14:12
浏览 27
已采纳

在不知道底层文件系统的情况下在PHP中加载文件

One of the ncie things about PHP is that if you do an include/require it works off the directory where the file is (or rather the URI of the request). That means that if you want to include further file you don't need to know from where the original script is running. All you need to know is where the current script is.

Now I want to load a text file and process it. file_get_contents() seems an obvioius way of loading it. The problem is I don't have that same luxury. I either need to specify the file path absolutely, which I don't want to do, or I have to know the location relative to the file that initiated the call, which I don't like either.

Require/include don't seem to work with non-PHP files and that would essentially echo the contents anyway (yes I know I could wrap that in an ob_start(), etc).

Suggestions?

  • 写回答

10条回答 默认 最新

  • duanjie2940 2009-02-06 14:21
    关注

    You can get the directory associated with the current file using

    dirname(__FILE__)
    

    So you could do something like this:

    file_get_contents(dirname(__FILE__) . '/foo.txt')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题