php - 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.
解决方案
你有一个印刷错误:
$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。
推荐阅读
- java - 构建 android 应用程序:我可以将数据发送到 localhost 中的数据库,但在在线数据库中不一样
- python - Python 3.7.4 中未定义写入
- python - 如何检测3种不同颜色的交集
- python - 可中断的多处理池工作者(python)
- google-cast - 如何使通知按钮无效或刷新
- google-analytics - Google Analytics - GTag.js:分析报告中未报告一些非自定义
- c# - 处理无效演员表 - IList to List
- machine-learning - 期刊文章中报告了哪种准确性(培训或测试)?
- php - Laravel 使用键返回对象:值而不是对象数组
- c++14 - c++14中带有struct的模板特化