mkdir 创建一个文件而不是目录
假设我们有以下树列表:
www _
\_sources_
\ \_dir1
\ \_dir2
\ \_file
\_cache
我试图递归地解析“源”中的每个文件并将其复制到保存层次结构的“缓存”文件夹中,但在我的函数 mkdir() 中创建一个文件而不是目录。 在函数之外,mkdir() 可以正常工作。这是我的函数:
function extract_contents ($path) {
$handle = opendir($path);
while ( false !== ($file = readdir($handle)) ) {
if ( $file !== ".." && $file !== "." ) {
$source_file = $path."/".$file;
$cached_file = "cache/".$source_file;
if ( !file_exists($cached_file) || ( is_file($source_file) && (filemtime($source_file) > filemtime($cached_file)) ) ) {
file_put_contents($cached_file, preg_replace('/<[^>]+>/','',file_get_contents($source_file)) ); }
if ( is_dir($source_file) ) {
# Tried to save umask to set permissions directly – no effect
# $old_umask = umask(0);
mkdir( $cached_file/*,0777*/ );
if ( !is_dir( $cached_file ) ) {
echo "S = ".$source_file."<br/>"."C = ".$cached_file."<br/>"."Cannot create a directory within cache folder.<br/><br/>";
exit;
}
# Setting umask back
# umask($old_umask);
extract_contents ($source_file);
}
}
}
closedir($handle);
}
extract_contents("sources");
PHP 调试只给了我[phpBB 调试] PHP 注意:在文件 /var/srv/shalala-tralala.com/www/script.php 第 88 行: mkdir() [function.mkdir]: ???? ?????????
没有其他行包含 mkdir()。
ls -l cache/sources
看起来像-rw-r--r-- 1 apache Apache 3 月 31 日 8 点 08:46 文件
-rw-r--r-- 1 apache apache 0 三月 31 日 08:46 dir1
很明显, mkdir() 创建了一个目录,但它没有为其设置“d”标志。我就是不明白,为什么。因此,第一次,有人可以帮助告诉我,如何通过 chmod() 通过八进制权限设置该标志,而我没有看到任何更好的解决方案? (我已经看过 man 2 chmod
和 man 2 mkdir
,没有关于“d”标志的内容)
补充:
通过将第二个 if 条件更改为
来解决 if ( (!file_exists($cached_file) && is_file($source_file)) || ( is_file($source_file) && (filemtime($source_file) > filemtime($cached_file)) ) )
Assume we have the following tree list:
www _
\_sources_
\ \_dir1
\ \_dir2
\ \_file
\_cache
I'm trying to recursively parse each file in the “sources” and copy it into “cache” folder saving the hierarchy, but in my function mkdir() creates a file instead of directory. Outside the function, mkdir() works properly. Here is my function:
function extract_contents ($path) {
$handle = opendir($path);
while ( false !== ($file = readdir($handle)) ) {
if ( $file !== ".." && $file !== "." ) {
$source_file = $path."/".$file;
$cached_file = "cache/".$source_file;
if ( !file_exists($cached_file) || ( is_file($source_file) && (filemtime($source_file) > filemtime($cached_file)) ) ) {
file_put_contents($cached_file, preg_replace('/<[^>]+>/','',file_get_contents($source_file)) ); }
if ( is_dir($source_file) ) {
# Tried to save umask to set permissions directly – no effect
# $old_umask = umask(0);
mkdir( $cached_file/*,0777*/ );
if ( !is_dir( $cached_file ) ) {
echo "S = ".$source_file."<br/>"."C = ".$cached_file."<br/>"."Cannot create a directory within cache folder.<br/><br/>";
exit;
}
# Setting umask back
# umask($old_umask);
extract_contents ($source_file);
}
}
}
closedir($handle);
}
extract_contents("sources");
PHP debug gives me nothing but[phpBB Debug] PHP Notice: in file /var/srv/shalala-tralala.com/www/script.php on line 88: mkdir() [function.mkdir]: ???? ??????????
There is no other lines which are containing mkdir().
ls -l cache/sources
looks like-rw-r--r-- 1 apache apache 8 Mar 31 08:46 file
-rw-r--r-- 1 apache apache 0 Mar 31 08:46 dir1
It's obvious, that mkdir() creates a directory, but it doesn't set "d" flag for it. I just can't understand, why. So at first time, can somebody help and tell me, how to set that flag through octal permissions via chmod(), while i don't see any better solution? (I've already seen man 2 chmod
and man 2 mkdir
, there is nothing about "d" flag)
addition:
Solved by changind the second if condition toif ( (!file_exists($cached_file) && is_file($source_file)) || ( is_file($source_file) && (filemtime($source_file) > filemtime($cached_file)) ) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用它:
它创建一个名为
$cached_file
的文件。然后,调用它:
在那里,您尝试创建一个名为
$cached_file
的目录。但已经存在一个具有该名称的文件。
这意味着:
mkdir
失败,因为存在一个具有该名称的文件file_put_contents
创建的文件。在评论后编辑:作为测试,我将尝试使用命令行而不是从 PHP 创建一个文件和一个同名目录,以确保 PHP 不会产生任何影响关于这一点。
首先让我们创建一个文件:
现在,我尝试创建一个同名的目录
a.txt
:错误消息(抱歉,我的系统是法语) 说“无法创建目录 a.txt:文件已存在”
那么,您确定可以创建与现有文件同名的目录吗?
You are using this :
Which creates a file called
$cached_file
.And, then, calling that :
There, you try to create a directory called
$cached_file
.But there is already an existing file with that name.
Which means :
mkdir
fails, as there is an file with that namefile_put_contents
.Edit after the comment : just as a test, I'll try creating a file, and a directory with the same name -- using command-line, and not from PHP, to make sure PHP doesn't have any impact on this.
First let's create a file :
And, now, I try creating a directory with the same name
a.txt
:The error message (sorry, my system is in french) says "impossible to create the directory a.txt : the file already exists"
So, are you sure you can create a directory with the same name as an existing file ?