【问题标题】:mkdir creates a file instead of directorymkdir 创建一个文件而不是目录
【发布时间】:2011-07-26 15:00:52
【问题描述】:

假设我们有以下树列表:

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 Debug] PHP Notice: in file /var/srv/shalala-tralala.com/www/script.php on line 88: mkdir() [function.mkdir]: ???? ?????????? 没有其他行包含 mkdir()。

ls -l cache/sources 看起来像
-rw-r--r-- 1 apache apache 8 Mar 31 08:46 file
-rw-r--r-- 1 apache apache 0 Mar 31 08:46 dir1
很明显,mkdir() 创建了一个目录,但它没有为它设置“d”标志。我就是不明白,为什么。所以第一次,有人可以帮助并告诉我,如何通过 chmod() 通过八进制权限设置该标志,而我没有看到任何更好的解决方案? (我已经看过man 2 chmodman 2 mkdir,没有什么关于“d”标志的)

补充:
通过将第二个 if 条件更改为
if ( (!file_exists($cached_file) &amp;&amp; is_file($source_file)) || ( is_file($source_file) &amp;&amp; (filemtime($source_file) &gt; filemtime($cached_file)) ) )

解决

【问题讨论】:

    标签: php file directory mkdir


    【解决方案1】:

    你正在使用这个:

    file_put_contents($cached_file, preg_replace('/<[^>]+>/','',file_get_contents($source_file)) ); }
    

    创建了一个名为$cached_file的文件。


    然后,调用它:

    mkdir( $cached_file/*,0777*/ );
    

    在那里,您尝试创建一个名为$cached_file的目录。

    但是已经有一个具有该名称的现有文件。
    这意味着:

    • mkdir 失败,因为有一个同名的文件
    • 并且您有一个文件,即您之前使用 file_put_contents 创建的文件。



    在评论后编辑:作为测试,我将尝试创建一个文件和一个同名目录——使用命令行而不是 PHP,以确保 PHP 没有任何影响关于这个。

    首先让我们创建一个文件:

    squale@shark: ~/developpement/tests/temp/plop 
    $ echo "file" > a.txt
    squale@shark: ~/developpement/tests/temp/plop 
    $ ls
    a.txt
    

    现在,我尝试创建一个同名的目录a.txt

    squale@shark: ~/developpement/tests/temp/plop 
    $ mkdir a.txt
    mkdir: impossible de créer le répertoire «a.txt»: Le fichier existe
    

    错误信息(对不起,我的系统是法语)“无法创建目录a.txt:文件已经存在”

    那么,您确定可以创建一个与现有文件同名的目录吗?

    【讨论】:

    • 当 mkdir 失败时,它不会创建任何内容。但它会创建一个文件。在同一个目录下有同名的文件和文件夹是没有问题的。此外,脚本现在停止在目录上,该目录本身附近没有同名文件。正如我上面写的,我试图在函数之外创建一个同名的目录并且它可以工作。但它什么也没显示,但我是个白痴。
    • 对不起,你当然是对的。不能有同名的文件和目录。我只有像“abc.def”这样的文件,并且对应于它们的文件夹“abc”,这很有效。看来,您也是对的,这是 file_put_contents 生成的文件,之前 mkdir 尝试这样做。通过再添加一个条件,它现在可以正常工作了。谢谢!