Jump to content

creating a new folder and files Using PHP AND inserting the contents.


seany123

Recommended Posts

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?

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!

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.