ディレクトリを作成し、そのディレクトリ内に別のディレクトリを作成するようにphpに指示するにはどうすればよいですか?
ここでは mkdir を使用しています。imagesというフォルダがあります。「user」という画像内にフォルダーを作成し、次に「15」というユーザーの下にフォルダーを作成する必要があります。user というフォルダーを一度に作成できます。どうすれば両方を一緒に行うことができますか?
ディレクトリを作成し、そのディレクトリ内に別のディレクトリを作成するようにphpに指示するにはどうすればよいですか?
ここでは mkdir を使用しています。imagesというフォルダがあります。「user」という画像内にフォルダーを作成し、次に「15」というユーザーの下にフォルダーを作成する必要があります。user というフォルダーを一度に作成できます。どうすれば両方を一緒に行うことができますか?
はい、recursive
パラメータをtrue
;として渡すことができます。
mkdir('images/user/15', 0777, true);
mkdir関数の再帰フラグを使用する
関数のシグネチャは次のとおりです。
bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
だからそれをそのように使う
mkdir('images/user/15',0777,true);
777モードを使用しないこともお勧めしますが、それは別の問題です。
試すmkdir('path/to/file', 0777, true);
mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
$the_path = '/user/15';
$the_mode = '0700';
mkdir($the_path,$the_mode, true);
新しいディレクトリに必要なパスとアクセス許可を生成し、「再帰」フラグを true に設定して mkdir 関数に渡すことができます。
このコードは、0777 のように定義したのと同じ権限で再帰的にディレクトリを作成します
umask(0777);
mkdir('images/user/15', 0777, true);
$newdir="user";
$subdir="15";
//fetch the current working directory
$curdir= getcwd();
// append the "images" directory to your current working directory
$dir=$curdir."\images";
// append the "$newdir" directory to your image directory path
$path=$dir."/$newdir";
// for the two line u can write $dir= $curdir."\images"."/$newdir";
// check if file exits
if(is_dir($path)) //or using the single line code if(is_dir($dir))
{
echo "directory exists";
}
else
{
mkdir($path."/$subdir",0777,true);
echo " directory Created";
}