如何解决问题 :: file_get_contents(http://localhost:8085/assets/data/test.txt): 无法打开流: HTTP 请求失败

发布于 2025-01-13 03:24:20 字数 1450 浏览 2 评论 0原文

通过使用 CodeIgniter 4 框架,我开发了RESTful api,我需要访问文件(.json 和 .txt)来获取内容。但无法访问 php 内置函数 file_get_contents()。 有关更多详细信息,请检查随附的屏幕截图 API_curl_file_get_content_error.PNG 输入图片这里的描述

并且 test.txt 文件也可以通过相同的文件路径访问。欲了解更多详细信息,请检查截图Input-txt-file-content.png 输入图片这里的描述

注意: 1) test.txt 文件和各自的目录具有完全权限。 2) 开发环境: <---->Apache/2.4.47 (Win64) OpenSSL/1.1.1k PHP/8.1.2 <---->数据库客户端版本:libmysql - mysqlnd 8.1.2 <---->PHP版本:8.1.2 <---->CodeIgniter 4

Product.php(index 方法)
<?php

namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;

class Product extends ResourceController
{
    use \CodeIgniter\API\ResponseTrait;

    public function index()
    {
        helper("filesystem");
        ini_set('max_execution_time', 300);

        $data['msg'] = "Product Index wizard";
        $data['status'] = 200;
        $file_path = base_url() . '/assets/data/test.txt';   // Read JSON
        $json = file_get_contents($file_path, true);
        $json_data = json_decode($json, true);

        return $this->respond($data);
    }
}

By using CodeIgniter 4 framework, I've developed RESTful api and there I need to access file (.json and .txt) to get content. But not able to access php inbuilt function file_get_contents().
For more details, pls check attached screenshot API_curl_file_get_content_error.PNG
enter image description here

And test.txt file is also accessible with same file path. For more details pls check screenshot Input-txt-file-content.png
enter image description here

NOTE : 1) test.txt file and respective directories have full permission.
2) Development environment :
<---->Apache/2.4.47 (Win64) OpenSSL/1.1.1k PHP/8.1.2
<---->Database client version: libmysql - mysqlnd 8.1.2
<---->PHP version: 8.1.2
<---->CodeIgniter 4

Product.php (index method)
<?php

namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;

class Product extends ResourceController
{
    use \CodeIgniter\API\ResponseTrait;

    public function index()
    {
        helper("filesystem");
        ini_set('max_execution_time', 300);

        $data['msg'] = "Product Index wizard";
        $data['status'] = 200;
        $file_path = base_url() . '/assets/data/test.txt';   // Read JSON
        $json = file_get_contents($file_path, true);
        $json_data = json_decode($json, true);

        return $this->respond($data);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2025-01-20 03:24:20

说明:

请记住,“http://localhost:8085/”很可能指向项目的文档根,通常是/public路径。因此,除非“assets”文件夹位于 /public 路径中,file_get_contents("http://localhost:8085/assets/data/test.txt "); 将无法找到请求的服务器资源。

解决方案:

由于您的文件资源 (test.txt) 位于本地文件系统上,

因此不要:

file_get_contents("http://localhost:8085/assets/data/test.txt");

使用以下内容:
常量ROOTPATH

项目根目录的路径。就在 APPPATH 上方。

file_get_contents(ROOTPATH . "assets/data/test.txt");

附录:

我相信您还忘记将 $json_data 输出添加到 Product::index 资源控制器方法中返回的 $data 变量。

Explanation:

Remember that "http://localhost:8085/" most likely points to the document root of the project, which is usually the /public path. So, unless the "assets" folder resides in the /public path, file_get_contents("http://localhost:8085/assets/data/test.txt"); will fail to find the requested server resource.

Solution:

Since your file resource (test.txt) is on the local filesystem,

Instead of:

file_get_contents("http://localhost:8085/assets/data/test.txt");

Use this:
constant ROOTPATH

The path to the project root directory. Just above APPPATH.

file_get_contents(ROOTPATH . "assets/data/test.txt");

Addendum:

I believe you also forgot to add the $json_data output to the returned $data variable in the Product::index resource controller method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文