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
https://forums.phpfreaks.com/topic/253414-writing-to-a-file/
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
https://forums.phpfreaks.com/topic/253414-writing-to-a-file/#findComment-1299018
Share on other sites

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.