Jump to content

Recommended Posts

If you're looking to have a 1-time install function that writes a php file, take a look at:

 

http://us.php.net/manual/en/ref.filesystem.php

 

Have your system check for the existence of a config.php file. If it's not found they're redirected to the installation page, which builds and saves that config.php, and redirects them back to where they started.

 

If you're looking to execute code dynamically, ie: pull some code out of a database and execute it, take a look at the eval() function.

 

Hope this helps...

 

 

cheers

Could anyone give me an abriged example of building a config file (I've done the form which then checks the connection,) just dont know how to get the POST data into a file which has the following in:

 

<?php
$dbhost = 
$dbuser = 
$dbpass = 
$dbname = 
$tbl_name = 
?>

 

Thanks

fopen and fwrite ;-)

 

The fopen function needs two important pieces of information to operate correctly. First, we must supply it with the name of the file that we want it to open. Secondly, we must tell the function what we plan on doing with that file.

 

Since we want to create a file, we must supply a file name and tell PHP that we want to write to the file. Note: We have to tell PHP we are writing to the file, otherwise it will not create a new file.

PHP Code:

 

$ourFileName = "config.php";

$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");

fclose($ourFileHandle);

 

The file "config.php" should be created in the same directory where this PHP code resides. PHP will see that "config.php" does not exist and will create it after running this code.

 

Now we can use the fwrite command to add data to our file. We would do this as shown below:

 

<?php

$File = "YourFile.txt";

$Handle = fopen($File, 'w');

$Data = "Jane Doe\n";

fwrite($Handle, $Data);

$Data = "Bilbo Jones\n";

fwrite($Handle, $Data);

print "Data Written";

fclose($Handle);

?>

Wit

fopen and fwrite ;-)

 

The fopen function needs two important pieces of information to operate correctly. First, we must supply it with the name of the file that we want it to open. Secondly, we must tell the function what we plan on doing with that file.

 

Since we want to create a file, we must supply a file name and tell PHP that we want to write to the file. Note: We have to tell PHP we are writing to the file, otherwise it will not create a new file.

PHP Code:

 

$ourFileName = "config.php";

$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");

fclose($ourFileHandle);

 

The file "config.php" should be created in the same directory where this PHP code resides. PHP will see that "config.php" does not exist and will create it after running this code.

 

Now we can use the fwrite command to add data to our file. We would do this as shown below:

 

    <?php

    $File = "YourFile.txt";

    $Handle = fopen($File, 'w');

    $Data = "Jane Doe\n";

    fwrite($Handle, $Data);

    $Data = "Bilbo Jones\n";

    fwrite($Handle, $Data);

    print "Data Written";

    fclose($Handle);

    ?>

 

With that added. Now all you need to do is include the config.php into where ever it's suppose to go.

 

Reminder: Becuase the config.php will not exist and/or not have the proper variables assigned, you need to create a check, otherwise you will run into some errors.

Wit

fopen and fwrite ;-)

 

The fopen function needs two important pieces of information to operate correctly. First, we must supply it with the name of the file that we want it to open. Secondly, we must tell the function what we plan on doing with that file.

 

Since we want to create a file, we must supply a file name and tell PHP that we want to write to the file. Note: We have to tell PHP we are writing to the file, otherwise it will not create a new file.

PHP Code:

 

$ourFileName = "config.php";

$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");

fclose($ourFileHandle);

 

The file "config.php" should be created in the same directory where this PHP code resides. PHP will see that "config.php" does not exist and will create it after running this code.

 

Now we can use the fwrite command to add data to our file. We would do this as shown below:

 

    <?php

    $File = "YourFile.txt";

    $Handle = fopen($File, 'w');

    $Data = "Jane Doe\n";

    fwrite($Handle, $Data);

    $Data = "Bilbo Jones\n";

    fwrite($Handle, $Data);

    print "Data Written";

    fclose($Handle);

    ?>

 

With that added. Now all you need to do is include the config.php into where ever it's suppose to go.

 

Reminder: Becuase the config.php will not exist and/or not have the proper variables assigned, you need to create a check, otherwise you will run into some errors.

 

and you can do that like this

 

<?php

$filename = '/pathto/config.php';

 

if (file_exists($filename)) {

echo "The file $filename exists";

} else {

echo "The file $filename does not exist";

}

?>

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.