Can we create folder with PHP code? I want that whenever a new user create his new account his folder automatically creates and a PHP file also created. Is this possible?
The mkdir() function creates a directory specified by a pathname.
Methods: file_exists(): It is an inbuilt function that is used to check whether a file or directory exists or not. is_dir(): It is also used to check whether a file or directory exists or not. mkdir() : This function creates a directory.
<?php mkdir("testing"); ?>
<= this, actually creates a folder called "testing".
<?php $file = fopen("test.txt","w"); echo fwrite($file,"Hello World. Testing!"); fclose($file); ?>
Use the a
or a+
switch to add/append to file.
<?php // change the name below for the folder you want $dir = "new_folder_name"; $file_to_write = 'test.txt'; $content_to_write = "The content"; if( is_dir($dir) === false ) { mkdir($dir); } $file = fopen($dir . '/' . $file_to_write,"w"); // a different way to write content into // fwrite($file,"Hello World."); fwrite($file, $content_to_write); // closes the file fclose($file); // this will show the created file from the created folder on screen include $dir . '/' . $file_to_write; ?>
You can create a directory with PHP using the mkdir() function.
mkdir("/path/to/my/dir", 0700);
You can use fopen() to create a file inside that directory with the use of the mode w
.
fopen('myfile.txt', 'w');
w : Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
You can create it easily:
$structure = './depth1/depth2/depth3/';
if (!mkdir($structure, 0, true)) {
die('Failed to create folders...');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With