次のツリーリストがあると仮定します。
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/sources
mkdir
-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)) ) )