file_put_contents(filepath/file)[function.file put contents]


file_put_contents(filepath/file) [function.file-put-contents]: failed to open stream: Permission denied

我已经在这里的相关论坛上探索并获得了一些技巧,但我仍然无法解决我的问题,所以我发布它来帮助我。谢谢大家。

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

我特别使用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/includs/../docs/gbo/data_memdata/someusr.dat([function.file-put-contents]:无法打开流:权限被拒绝

我问他们我的用户帐户是否属于该文件分配给的具有写入权限的组的一部分。我的目录是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所有权。

如果强制设置为活动,这可能是个问题。

尝试(在Linux shell上(:[root@*]#setenforce 0

为我解决了这个问题。