Jump to content

Recommended Posts

I have a code that I use to write a php file.  It is below.

 

<?php
$myFile = "test2.php";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "<?php
define('CONTIN', '1');
define('THEME', 'NullByt3');
define('TITLE','Test Null Byt3 Site');
define('FORUM_NAME', 'forums');
define('LOGOUT_TEXT', 'Log Out');
define('LOGIN_TEXT', 'Log In');
define('PROFILE_TEXT','Profile');
$db_user = 'g5gbocom_admin';
$db_host = 'localhost';
$db_pwd = '(Computer)';
$db_name = 'g5gbocom_null';
$db_prefix = 'xfrm_';
session_start();
?>";
fwrite($fh, $stringData);
fclose($fh);
?>

 

It works pretty well, exept for the last part of the file it's supposed to write.  Instead of looking like this:

 

<?php
define('CONTIN', '1');
define('THEME', 'NullByt3');
define('TITLE','Test Null Byt3 Site');
define('FORUM_NAME', 'forums');
define('LOGOUT_TEXT', 'Log Out');
define('LOGIN_TEXT', 'Log In');
define('PROFILE_TEXT','Profile');
$db_user = 'g5gbocom_admin';
$db_host = 'localhost';
$db_pwd = '(Computer)';
$db_name = 'g5gbocom_null';
$db_prefix = 'xfrm_';
session_start();
?>";

 

It writes the file like this:

<?php
define('CONTIN', '1');
define('THEME', 'NullByt3');
define('TITLE','Test Null Byt3 Site');
define('FORUM_NAME', 'forums');
define('LOGOUT_TEXT', 'Log Out');
define('LOGIN_TEXT', 'Log In');
define('PROFILE_TEXT','Profile');
= 'g5gbocom_admin';
= 'localhost';
= '(Computer)';
= 'g5gbocom_null';
= 'xfrm_';
session_start();
?>";

 

I'm sorry if this is confusing.  I'd appreciate any help.

Link to comment
https://forums.phpfreaks.com/topic/62924-file-write-issues/
Share on other sites

because you're using double quotes, PHP is trying to interpolate those variable names as actual variables, which have no value and thus turn up blank in the string.  escape the $ with a backslash in order to tell PHP that you mean a literal dollar sign, not a variable indicator.

Link to comment
https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313273
Share on other sites

Whever you have a variable in the string, break out of the string using this for example:

 

".$variablename."

 

Ed.

 

still won't help, as that will still attempt to interpolate the variable name as a variable, rather than simply as a string.

Link to comment
https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313282
Share on other sites

I appologize for asking for more help so soon, but I'm stuck.

 

If I wanted to use the same script, but instead of:

define('TITLE','Test Null Byt3 Site');

use something like:

define('TITLE',$_REQUEST["title"]);

to write file data, how would I do that?  When I tried this code, it wouldn't even write the file.

 

Basically I have a form that a user fills out, and in the action, is the file with this code in it.  Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313297
Share on other sites

ah, i figured you meant when the configuration script runs you need it to contain the title.  this is just a matter of interpolating the variable - you can enclose arrays in braces to have it evaluate as such:

 

define('TITLE', '{$_REQUEST['title']}')

 

you'll need to be careful about the variable's treatment of quotes in this.

Link to comment
https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313309
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.