0

次のツリーリストがあると仮定します。

www _
     \_sources_
      \        \_dir1
       \        \_dir2
        \        \_file
         \_cache

「sources」内の各ファイルを再帰的に解析し、階層を保存する「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/sourcesmkdir
-rw-r--r-- 1 apache apache 8 Mar 31 08:46 file
-rw-r--r-- 1 apache apache 0 Mar 31 08:46 dir1
()がディレクトリを作成するのは明らかですが、「d」フラグは設定されていません。どうしてかわからない。それで、最初に、誰かが助けてくれて、chmod()を介して8進数のパーミッションを介してそのフラグを設定する方法を教えてもらえますが、より良い解決策はありませんか?(私はすでに見ましたman 2 chmod、そしてman 2 mkdir、「d」フラグについては何もありません)

追加:
2番目のif条件を次のように変更することで解決
if ( (!file_exists($cached_file) && is_file($source_file)) || ( is_file($source_file) && (filemtime($source_file) > filemtime($cached_file)) ) )

4

1 に答える 1

4

あなたはこれを使用しています:

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を作成できません:ファイルは既に存在します」と表示されます。

では、既存のファイルと同じ名前のディレクトリを作成できると思いますか?

于 2011-03-31T05:38:28.397 に答える