Creating a new file

I have a file named “createFile.php” for creating a new file in the same directory in which the file “createFile.php” is.

What code should I put in “createFile.php” for creating a new file named “newFile.php”?

file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'newFile.php', 'contents-of-the-file');

Your code works fine.
Thank you, Avram.

By the way,
if the directory which has the file createFile.php has a sub-directory named “my_subDirectory”,
How can I put a “newFile.php” into “my_subDirectory”?

The trial code below doesn’t work correctly, but I hope it shows what I want.

file_put_contents(dirname(__my_subDirectory__) . '/' . 'newFile.php', 'contents-of-the-file_in_my_subDirectory');

Try this:

file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'my_subDirectory/newFile.php', 'contents-of-the-file');

Thank you again, Avram.

I have another question related on this.

I like to create a new directory instead of a new file.
How can I create a new directory named “newDirectory” in the directory which has the file “createDirectory.php”?

mkdir(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'newDirectory');

:slight_smile: