Jump to content

Writing to a file?


Shazer2

Recommended Posts

I'm trying to write to a file with the following code.

$fh = fopen("../inc/config.php", "a") or die("\r\nCan't open file.");
$write = "\$config['database_host'] = {$mysqlh};
\$config['database_user'] = {$mysqlu};
\$config['datbase_pass'] = {$mysqlp};
\$config['datbase_name'] = {$dbn};
\$config['table_prefix'] = {$tp};";

fwrite($fh, $write);
fclose($fh);

 

It is printing out "Can't open file", which I guess means it can't find the file or I've given the wrong path. The file that is executing this code is /install and the file I'm trying to write to is /inc/config.php. I thought .. put you up a directory so if I do .. I will be at the root and then do /inc I will be in inc.

 

Can someone please guide me on what I'm doing wrong?

 

Thanks.

Link to comment
Share on other sites

i would reccomend utilizing the absolute paths, so that it works for almost any server config...

Works like a charm for me.

<?php
$fh = fopen(dirname(dirname(__FILE__))."/inc/config.php", "a") or die("\r\nCan't open file.");
$write = "\$config['database_host'] = {$mysqlh};
\$config['database_user'] = {$mysqlu};
\$config['datbase_pass'] = {$mysqlp};
\$config['datbase_name'] = {$dbn};
\$config['table_prefix'] = {$tp};";

fwrite($fh, $write);
fclose($fh);
?>

Link to comment
Share on other sites

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.