TutorMe Posted August 1, 2007 Share Posted August 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/62924-file-write-issues/ Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313273 Share on other sites More sharing options...
a1phanumeric Posted August 1, 2007 Share Posted August 1, 2007 Swap all your speechmarks for single quotes AND single quotes for speechmarks OR Whever you have a variable in the string, break out of the string using this for example: ".$variablename." Ed. Quote Link to comment https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313277 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313282 Share on other sites More sharing options...
a1phanumeric Posted August 1, 2007 Share Posted August 1, 2007 Oops, my bad! :-X Quote Link to comment https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313283 Share on other sites More sharing options...
TutorMe Posted August 1, 2007 Author Share Posted August 1, 2007 Thanks to both of you, but akitchin's fix was what worked. I appreciate the quick response. Quote Link to comment https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313285 Share on other sites More sharing options...
TutorMe Posted August 1, 2007 Author Share Posted August 1, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313297 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313301 Share on other sites More sharing options...
TutorMe Posted August 1, 2007 Author Share Posted August 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313305 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313309 Share on other sites More sharing options...
TutorMe Posted August 1, 2007 Author Share Posted August 1, 2007 Thanks sooo much. That fixed it perfectly. I really appreciate your quick response. That is what helps make this site great. Quote Link to comment https://forums.phpfreaks.com/topic/62924-file-write-issues/#findComment-313313 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.