php - file_put_contents(文件路径/文件)[function.file-put-contents] : failed to open stream: Permission denied

标签 php linux file-io

我已经探索并从相关论坛中获取了一些提示,但我仍然无法解决我的问题,所以我发布它请帮助我。感谢大家。

我共享了 Linux 主机,最近他们提供了升级,我接受了。不幸的是,他们声称我的 php 应用程序应该微调到 php 5.3 版。我的这个应用程序在早期的 php 5.2 版服务器中运行良好。当我尝试注册登录或注销历史记录或任何此类文件写入时,问题是文件权限被拒绝错误。我突出显示了以下代码,因为它是遇到 sample 的第一个错误。这是我的示例代码。

我专门使用 file_put_contents 和 file_read_contents。下面是我的示例代码。

// define('DATA_PATH_MEMB', dirname( __FILE__) ."/../docs/vendor/"."data/_memdata/");
// above constant is an include file 
function updateLogins() {
  $user = $_SESSION['thisuserName'];
  $filejsonread = DATA_PATH_MEMB.$user.".dat";
  $filejsonwrite = DATA_PATH_MEMB.$user.".dat";  
  $updateDataArr = json_decode(file_get_contents($filejsonread),true);
    $timenow =  array( "datetimestamp" => date("d-m-Y h:i") );
    array_push($updateDataArr["useracess"][0]["history"]["logins"], $timenow);
    array_push($updateDataArr["useracess"][0]["history"]["logouts"], $timenow);
    file_put_contents($filejsonwrite, json_encode($updateDataArr), LOCK_EX);    
 } 

将 file_put_contents 指向以下错误

file_put_contents(/home/mydomain/public_html/includes/../docs/gbo/data/_memdata/someusr.dat) [function.file-put-contents]: failed to open stream: Permission denied

我问他们我的用户帐户是否属于文件分配给具有写入权限的组。我的目录是 755,我的文件是 644,在早期的服务器中,我的应用程序可以正常工作。如果无法写入任何文件,我不知道该怎么办。

我试过了

echo exec('whomai'); // it displays nobody 

我是程序员而不是管理员。我应该问他们什么或纠正自己。

最佳答案

文件有,

-rw-r--r-- (644) — Only the owner has read and write permissions; the group and others can read only.

此权限状态表示文件的所有者具有写权限,但其他任何人都没有。当您尝试写入此文件的 PHP 文件以与您的用户名不同的用户身份运行时。

因此出现错误,Apache 无法写入此文件。但是如果你 ssh 到你的服务器和 vi -file。您将能够毫无问题地写入该文件。

解决方案:

授予 Apache 对此文件的写权限。 chown 这个文件并赋予 Apache 所有权。

关于php - file_put_contents(文件路径/文件)[function.file-put-contents] : failed to open stream: Permission denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16451647/

相关文章:

php - 如何在 PHP 中编写非均匀随机分布代码

linux - "undefined reference to ` hdr_aodv::offset_ '"网络模拟器错误

linux - postgres 复制命令,二进制文件

android - LibGDX 无法加载特定图像

Php - 在数组内传递变量

php - 显示组是否可以访问某些表单

php - 如何在 PHP 中的 PHPDocumentor block 中粘贴特殊符号,例如 "*/"?

c - Fd[0] 之后的 dup2!= STDIN_FILENO

java - TomCat:使用类路径打开文件

c++ - C++中的多线程应用程序