简体   繁体   English

file_get_contents(/target/file/here/file.json): 无法打开 stream: Permission denied in homepage.php

[英]file_get_contents(/target/file/here/file.json): Failed to open stream: Permission denied in homepage.php

I'm trying to read a json file from my php code by using file_get_contents but I get "Failed to open stream: Permission denied", but it worked when I tried to make selinux in Permissive mode.我正在尝试使用 file_get_contents 从我的 php 代码中读取一个 json 文件,但我得到“无法打开 stream:权限被拒绝”,但是当我尝试在 Permissive 模式下制作 selinux 时它起作用了。 My is how I can allow file_get_contents to read files without turning on selinux in Permessive mode?我的是如何允许 file_get_contents 在不以 Permessive 模式打开 selinux 的情况下读取文件?

Note: I'm using httpd server in redhat 7注意:我在 redhat 7 中使用 httpd 服务器

Trying:试:

           $json = file_get_contents('/target/file/here/file.json');
           //modify json file
           file_put_contents('new_file.json', $Json_to_put);

Expecting:期待:

1- file_get_content read my file.json 2- file_put_contents create my new_file.json 1- file_get_content 读取我的 file.json 2- file_put_contents 创建我的 new_file.json

You can try你可以试试

  1. Change the owner of the webroot更改 webroot 的所有者

    chown -R apache:apache /target/file/here chown -R apache:apache /目标/文件/这里

  2. Change the basic permissions更改基本权限

    chmod -R g+w /target/file/here chmod -R g+w /目标/文件/这里

    chmod g+s /target/file/here chmod g+s /目标/文件/这里

  1. Permissions would need to be accessable权限需要可访问
//Partial Headers from a 3rd party JSON based API I have access too
"headers":
  {"Access-Control-Allow-Headers":"Content-Type,
   Access-Control-Allow-Headers, 
   Access-Control-Allow-Origin, 
   Access-Control-Allow-Methods",
   "Access-Control-Allow-Origin":"*",
   "Access-Control-Allow-Methods":"GET",
   "Content-Type":"application/json"}

You can see the access allows origin of "*", control-method of "GET"可以看到“*”的访问允许来源,“GET”的控制方法

Also - you may want to decode the json..另外 - 您可能想要解码 json..

$json = json_decode(file_get_content($url), false);
// False returns an Object and True returns an Array.

You're doing this from a file on the server, so this likely won't apply, but just for future reference - I've also run into problems when a 3rd party API (in JSON) is encoded, so watch for GZ Encoding , or other types of Encoding.您是从服务器上的文件执行此操作的,因此这可能不适用,但仅供将来参考-当对第 3 方 API(JSON 格式)进行编码时,我也遇到了问题,因此请注意GZ 编码,或其他类型的编码。 The link I used to grab the above info is GZ encoded.我用来获取上述信息的链接是 GZ 编码的。

You should then be able to edit the data and file_put_content the data back to the file.然后您应该能够编辑数据并将file_put_content数据返回到文件中。

When you create the file, be sure to add the proper permissions.创建文件时,请务必添加适当的权限。 PHP File Create/Write PHP 文件创建/写入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 file_get_contents():无法打开流:权限被拒绝 - file_get_contents(): failed to open stream: Permission denied PHP Drupal file_get_contents()无法打开流:权限被拒绝 - PHP Drupal file_get_contents() failed to open stream : permission denied 无法打开流:权限被拒绝生成XML文件的file_get_contents - failed to open stream: Permission denied file_get_contents which generates XML FILE Apache 服务器中的 file_get_contents() “无法打开 stream:权限被拒绝” - file_get_contents() “failed to open stream: Permission denied” in Apache server 无法在file_get_contents中打开流 - Failed to open stream in file_get_contents file_get_contents()无法打开流: - file_get_contents() failed to open stream: PHP:file_get_contents无法打开流:连接被拒绝 - PHP: file_get_contents failed to open stream: Connection refused PHP file_get_contents()无法打开流错误 - PHP file_get_contents() failed to open stream error PHP:调用具有json响应的页面时,file_get_contents(url)无法打开流 - php: file_get_contents(url) failed to open stream when calling a page that has a json response 错误无法打开流:file_get_contents()中没有此类文件或目录 - Error failed to open stream: No such file or directory in file_get_contents()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM