A few days ago to see someone asked how to create a file in the PHP environment under the specified directory, just their recent study, after a test, finally the results, posted out to share with you.
Directory structure:
The file where the code is located wwwroot/mydir/test/test.php
Create directory: Create a directory testjiang123 under wwwroot/mydir/.
Create file: Create a file test.html under wwwroot/mydir/testjiang123/.
- Create a folder
- $username = "testjiang123";
- $dir = ". /mydir/". $username;
- function Createdir ($dir)
- {
- if (file_exists ($dir) && Is_dir ($dir)) {
- }
- else{
- mkdir ($dir, 0777);
- }
- Creat_file ($dir);
- }
- Create a file
- function Creat_file ($path) {
- $sFile = "test.html";
- $var _content= "File contents";
- if (File_exists ($path. " /". $sFile)) {
- Creat_file ();
- } else {
- $fp = fopen ($path. " /". $sFile," w ");
- Fwrite ($fp, $var _content);
- Fclose ($FP);
- }
- return $sFile;
- }
- Createdir ($dir);
- A PHP copy function is also added here, copy the mytest.php in the current directory to the directory you just created, and rename it to the directory name +_mytest.php
- copy ("mytest.php", $dir. " /". $username." _mytest.php ");