Jump to content

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


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!

?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.