【问题标题】:How to "readfile" in Beanstalk PHP如何在 Beanstalk PHP 中“读取文件”
【发布时间】:2026-01-07 04:15:02
【问题描述】:

我已将我的项目压缩并上传到 AWS Beanstalk。然后我必须手动配置Document root,即:/api-test-beanstalk

我的索引文件位于这个目录中,所以:

zip --> api-test-beanstalk --> index.php

在 index.php 内部我试图读取位于同一个文件夹中名为 abc.php 的文件,所以我尝试了:

$file = '/api-test-beanstalk/abc.php';
echo file_get_contents($file);

$file = 'api-test-beanstalk/abc.php';
echo file_get_contents($file);

$file = '/abc.php';
echo file_get_contents($file);

$file = 'abc.php';
echo file_get_contents($file);

似乎没有任何效果,所有结果都会导致如下所示的错误:

Warning: file_get_contents(api-test-beanstalk/abc.php): failed to open stream: No such file or directory in /var/app/current/api-test-beanstalk/index.php on line 67

如何在 index.php 中读取 abc.php?这些文件在同一个文件夹中彼此相邻。

【问题讨论】:

  • 您是否将 abc.php 与 index.php 一起提交?
  • @Tal 是的,它们位于 ZIP 文件夹中名为 api-test-beanstalk 的同一个文件夹中
  • 没关系,找到答案了

标签: php amazon-elastic-beanstalk file-get-contents readfile


【解决方案1】:

您需要指定绝对路径

$file = './abs.php'; 
readfile($file);

注册路径:参考这个LINK

【讨论】:

  • 我宁愿不做一个 http 请求,因为文件就在它旁边,这似乎有点矫枉过正。您可能知道一些解决方法吗?
  • 找到了答案:$file = './abs.php'; readfile($file); 谢谢会接受
  • 如果您的 index.phpabc.php 在同一个文件夹中,它肯定会起作用。我在本地测试过
  • 它在 Beanstalk 上不起作用,没关系我找到了答案,上面的代码。感谢您的宝贵时间
最近更新 更多