$path="dir1";
mkdir("$path",0777);
$path1="dir1/dir2";
mkdir("$path1",0777);
$path2="dir1/dir2/dir3";
mkdir("$path2",0777);
$percorso=$path2;
$var=fopen($percorso."/nome_file.txt","a+");
fwrite($var, "stringa di prova");
php create file in a new directory
-
19-10-2022 - |
Question
I need create a new txt file in a new directory. I do thi code
$path="dir1";
mkdir("$path",0777);
$path1="dir1/dir2";
mkdir("$path1",0777);
$path2="dir1/dir2/dir3";
mkdir("$path2",0777);
$percorso=$path.$path1.$path2;
$var=fopen($percorso."/nome_file.txt","a+");
fwrite($var, "stringa di prova");
It creates the 3 directory dir1/dir2/dir3 on my server but not the file. why?
Solution
OTHER TIPS
$path2 witll be sufficient
$var=fopen($path2."/nome_file.txt","a+");
Because if $path="dir1"
and $path1="dir1/dir2"
and $path2="dir1/dir2/dir3"
, the code $percorso=$path.$path1.$path2
will give you :
dir1dir1/dir2dir1/dir2/dir3
Just change
$var=fopen($percorso."/nome_file.txt","a+");
to
$var=fopen($path2."/nome_file.txt","a+");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow