seany123 Posted July 5, 2011 Share Posted July 5, 2011 Is it possible to create a folder and php files in a php script? also im wanting to define the contents of the file.. which could obviously be php code. Quote Link to comment https://forums.phpfreaks.com/topic/241163-creating-a-new-folder-and-files-using-php-and-inserting-the-contents/ Share on other sites More sharing options...
Cagecrawler Posted July 5, 2011 Share Posted July 5, 2011 It is possible, look at mkdir and the fopen family of functions. Any file you write to can be used the same as any other file on the filesystem (you can use include for example). Quote Link to comment https://forums.phpfreaks.com/topic/241163-creating-a-new-folder-and-files-using-php-and-inserting-the-contents/#findComment-1238741 Share on other sites More sharing options...
seany123 Posted July 5, 2011 Author Share Posted July 5, 2011 It is possible, look at mkdir and the fopen family of functions. Any file you write to can be used the same as any other file on the filesystem (you can use include for example). okay so them 2 functions actually create the folders and file... but what about inserting code into the files? Quote Link to comment https://forums.phpfreaks.com/topic/241163-creating-a-new-folder-and-files-using-php-and-inserting-the-contents/#findComment-1238743 Share on other sites More sharing options...
AbraCadaver Posted July 5, 2011 Share Posted July 5, 2011 mkdir('/path/to/dir'); $stuff = 'some stuff here what ever you want'; file_put_contents('/path/to/dir/file.txt', $stuff); Quote Link to comment https://forums.phpfreaks.com/topic/241163-creating-a-new-folder-and-files-using-php-and-inserting-the-contents/#findComment-1238744 Share on other sites More sharing options...
Chris92 Posted July 5, 2011 Share Posted July 5, 2011 read the PHP documentation, he posted links on the functions. mkdir() makes a new directory: http://php.net/mkdir fopen() starts a new file stream: http://php.net/fopen once you have started a new stream you can write to it using fwrite(): http://php.net/fwrite and save it using fclose(): http://php.net/fclose I'll write you a small script: <?php $dir = 'myDir'; $file = 'myFile.php'; // check the directory to see if it already exists if( is_dir($dir) === false ) { // if it doesn't make a new directory mkdir($dir); } // start a new stream $f = fopen($file, 'w'); // 'w' for write // write the php to the stream fwrite( $f, '<?php echo "Hello world!"; ?>' ); // close the stream fclose($f); // include our new php script to see if it's working: include $myDir . '/' . $myFile; // Hello world! ?> Quote Link to comment https://forums.phpfreaks.com/topic/241163-creating-a-new-folder-and-files-using-php-and-inserting-the-contents/#findComment-1238745 Share on other sites More sharing options...
jcbones Posted July 6, 2011 Share Posted July 6, 2011 file_put_contents() is essentially calling fopen(), fwrite(), and fclose(), just in one function instead of 3. Quote Link to comment https://forums.phpfreaks.com/topic/241163-creating-a-new-folder-and-files-using-php-and-inserting-the-contents/#findComment-1238756 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.