Jump to content

File Write issues...


TutorMe

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
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
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
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
Share on other sites

you'd write it the same way you do the other variables, except you'd need to escape the offending characters:

 

define('TITLE', \$_REQUEST['title'])

 

you're supposed to use single quotes to designate a string key of an array anyway.

Link to comment
Share on other sites

you'd write it the same way you do the other variables, except you'd need to escape the offending characters:

 

define('TITLE', \$_REQUEST['title'])

 

Wouldn't this skip the "$REQUEST" part of the code?  That is code that I actually want to execute.

Link to comment
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
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.