mkdir(“dir”,0777)和chmod(“dir”,077)无效

问题描述 投票:0回答:1

简而言之,以下代码旨在创建一个目录结构,如:

>Attachments
  >Lot
    >Layer

附件目录是固定的。 Lot提供0777权限。 Layer目录没有。我担心也许umask出错了,但我没有改变任何东西。

// Create directory for this entry's attachments if needed.
  $attachment_dir = $config_ini['OOCDB_defaults']['attachment_dir'];
  $attachment_lot_dir = $attachment_dir.$txtLotID."/";
  $attachment_lot_layer_dir = $attachment_lot_dir . $txtLayer."/";


  if(!is_dir($attachment_lot_dir)){
      mkdir($attachment_lot_dir , 0777);
  }

  if(!is_dir($attachment_lot_layer_dir )){
      mkdir($attachment_lot_layer_dir , 0777);
  }

  chmod($attachment_lot_dir ,0777);
  chmod($attachment_lot_layer ,0777);   
  $sleuthFile = $attachment_lot_layer_dir . "makeSleuthImg.txt";
  $fp = fopen($sleuthFile,"w") or die("unable to open File! <br>");
  //Write the string to the file and close it.
php chmod mkdir
1个回答
1
投票

你有一个印刷错误:

$attachment_lot_layer_dir = $attachment_lot_dir . $txtLayer."/";
...
chmod($attachment_lot_layer ,0777);

那个变量不存在,所以是的,永远不会有效。 PHP的mkdir尊重Linux中的umask(假设您使用的是Linux,否则就不会发生这种情况),因此您的目录不会按照请求在0777掩码中创建;但是chmod不尊重umask,所以你第一次调用chmod实际上是将这个目录的掩码更改为0777.第二次调用由于变量名称错误而失败。因此,你看到的行为。

FWIW,mkdir有一个第二个可选的布尔参数,它允许你通过传递完整的目录路径在一次调用中递归创建一个目录结构(参见here)。你还应该查看this问题,以便在调用mkdir之前了解如何处理umask,如果你想避免完全后续调用chmod。

© www.soinside.com 2019 - 2024. All rights reserved.