Home >Backend Development >PHP Tutorial >put your head on my shoulder PHP obtains the directory under the specified directory and creates a file under the obtained directory. Multi-platform
Copy the code The code is as follows:
//Get the directory name of the specified folder
function get_dir_name($dir_path,$file)
{
$dirpath = $dir_path;
$dir = scandir($dirpath) ;
foreach ($dir as $key=>$value)
{
if (is_dir($dirpath.'/'.$value) && $value != '.' && $value != '..')
{
//echo $dirpath.'/'.$value.'/'.$file;
//Generate a config.php file in the directory. Of course, this file can be defined by yourself
if (!file_exists($ dirpath.'/'.$value.'/'.$file))
{
$fo = fopen($dirpath.'/'.$value.'/'.$file,'xb+'); //In Here, I tried to use w+ before, but the result failed.
//Under windows, I created successfully, please pay attention! All suggestions are to use xb+, compatible with multiple platforms
chmod($file, "0777");
fwrite ( $fo,'i is a zongzi ,here is config file!') or die('Failed to create configuration file! Please check whether you have the operating permissions for this directory and file!');
fclose($fo);
}
$arr[] = $value;
}
}
return $arr;
}
The above introduces how put your head on my shoulder PHP obtains the directory in the specified directory, and then creates the file under the obtained directory. It is multi-platform, including the content of put your head on my shoulder. I hope you are interested in PHP tutorials. Friends help.