【问题标题】:Folder and file permission issues on Mac OS XMac OS X 上的文件夹和文件权限问题
【发布时间】:2015-02-09 22:17:42
【问题描述】:

我编写了一个 PHP 脚本,它获取 MySQL 查询的结果,对结果进行 json_encode,最后执行 file_put_contents() 操作。在我的 PC 上运行我的 Bitnami WAMP 开发服务器时,脚本可以完美执行。但是,当在我的 Mac 上克隆我的 git 项目时(运行 Yosemite)。尝试执行 file_put_contents() 函数时,我的权限被拒绝。这是我的脚本:

<?php
// All Articles to JSON //
if( array_key_exists("makejson", $_REQUEST) ) {

// Class to Serialize The JSON
class ArrayValue implements JsonSerializable {
public function __construct(array $array) {
    $this->array = $array;
 }

 public function jsonSerialize() {
     return $this->array;
 }
}

// Designate the file
$file = "articles.json";

// FHA articles query
$milArticles = $dataConnection->SelectColsWhere( "text_news", "active='1' ORDER BY ndate DESC", "textnewsid,ndate,ntitle,sentence" );

if(count($milArticles) > 0){

  $json_data = json_encode( new ArrayValue($milArticles), JSON_HEX_APOS | JSON_PRETTY_PRINT );

  // Check for JSON errors
  if ($json_data === null && json_last_error() !== JSON_ERROR_NONE) {

   throw new \LogicException(sprintf("Failed to parse json string '%s', error: '%s'", $json_data , json_last_error_msg()));

    } else {

      file_put_contents($file, $json_data, LOCK_EX);


   }
 }
}// END OF MAKE JSON

这是我得到的错误:

[Mon Feb 09 16:06:20.522798 2015] [:error] [pid 686] [client 127.0.0.1:50195] PHP Warning:  file_put_contents(articles.json): failed to open stream: Permission denied in /Users/myuser/Sites/PIXEL/militaryinfo/restrict/includes/articleJson.php on line 33, referer: http://militaryinfo/restrict/submit_JSON.php

这是我要写入的目录中的权限:

drwxr-xr-x  15 myuser  staff   510 Feb  6 19:13 restrict

我听到的解决方案是在 PHP 中运行 unmask() 和 chmod(),但我都没有运气。即使在终端中运行 chmod 或 chown 似乎也无济于事。

【问题讨论】:

    标签: php macos apache


    【解决方案1】:

    PHP 脚本中运行 echo exec('whoami'); 并从浏览器运行 PHP 脚本。它为您提供了服务器应用程序的所有者。

    然后chown 该用户和组同时提供您希望的适当权限。比如:

    chown -R user:user /my/folder
    

    user 是从 PHP 脚本中发现的。

    【讨论】:

    • 看起来就像安装了 Mac OS X 的 Apache,所有文件/文件夹的所有者都是 _www。所以我只需要将用户更改为“myuser”。喜欢:chown -R myuser:user /my/folder ?
    • chown -R _www:_www /my/folder。然后,你可以做一个chmod -R 755 /my/folder。如果需要,请使用sudo
    • 我现在收到此错误:[Mon Feb 09 17:02:51.339966 2015] [core:notice] [pid 1095] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
    • 最终结果是发现我在我的变量'articles.json'中命名的文件存在,它取消链接该文件abd未能创建一个新的articles.json文件。
    猜你喜欢
    • 1970-01-01
    • 2014-12-20
    • 2012-08-16
    • 2016-05-13
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 2011-11-14
    相关资源
    最近更新 更多