首页 > 解决方案 > mkdir ("dir", 0777) 和 chmod ("dir", 077) 不工作

问题描述

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

>Attachments
  >Lot
    >Layer

附件目录是固定的。该批次带有 0777 权限。图层目录没有。在担心 umask 可能有问题后,我添加了 chmod 行,但它并没有改变任何东西。

// 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.

标签: phpchmodmkdir

解决方案


你有一个印刷错误:

$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 有第二个可选的布尔参数,它允许您通过将完整目录路径传递给它(参见此处),在单个调用中递归地创建目录结构。如果您想完全避免随后对 chmod 的调用,您还应该查看这个问题以了解在调用 mkdir 之前如何处理 umask。


推荐阅读