Jump to content

Recommended Posts

Inmy script I am trying to create and write to a new php file .. everything works great until I try to put in what I want to write ... I am trying to write PHP code to a php file ...

 

this is what I am trying to write to the pho file

 

// Change the 4 variables below
$yourName = 'change this to your name';
$yourEmail = 'you@yourdomain.com';
$yourSubject = 'change to your subject line';
$referringPage = 'http://www.yourdomain.com/contact.php';


// No need to edit below unless you really want to. It's using a simple php mail() function. Use your own if you want
function cleanPosUrl ($str) {
return stripslashes($str);
}
if ( isset($_POST['sendContactEmail']) )
{
$to = $yourEmail;
$subject = $yourSubject.': '.$_POST['posRegard'];
$message = cleanPosUrl($_POST['posText']);
$headers = "From: ".cleanPosUrl($_POST['posName'])." <".$_POST['posEmail'].">\r\n";
$headers .= 'To: '.$yourName.' <'.$yourEmail.'>'."\r\n";
$mailit = mail($to,$subject,$message,$headers);
	if ( @$mailit ) {
	header('Location: '.$referringPage.'?success=true');
	}
	else {
	header('Location: '.$referringPage.'?error=true');
	}
}

 

but since its code its throwing errors .. how can I put the above into a string and write it to a file ?

 

thx

 

 

Link to comment
https://forums.phpfreaks.com/topic/135553-solved-help-with-writing-to-a-file/
Share on other sites

Your main issue is going to be the use of both single and double quotes. Stick to one. That way you can put the whole thing into a string properly without having to escape the characters.

 

Chances are the errors are being thrown cause of " or ' single quotes. I would escape the double quotes around the \r\n since those need the double quotes.

 

Let me know if that fixes your issue.

this is my error ..

 

also  .what is the correct way to escape a character ...

 

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/franklin/public_html/sites/rssnewsite/install/step_03.php on line 32

 

<?php
$writeToFile = "// Change the 4 variables below
$yourName = 'change this to your name';
$yourEmail = 'you@yourdomain.com';
$yourSubject = 'change to your subject line';
$referringPage = 'http://www.yourdomain.com/contact.php';


// No need to edit below unless you really want to. It's using a simple php mail() function. Use your own if you want
function cleanPosUrl ($str) {
return stripslashes($str);
}
   if ( isset($_POST['sendContactEmail']) )
   {
   $to = $yourEmail;
   $subject = $yourSubject.': '.$_POST['posRegard'];
   $message = cleanPosUrl($_POST['posText']);
   $headers = 'From: '.cleanPosUrl($_POST['posName']).' <'.$_POST['posEmail'].\">\r\n\";
   $headers .= 'To: '.$yourName.' <'.$yourEmail.'>'.\"\r\n\";
   $mailit = mail($to,$subject,$message,$headers);
      if ( @$mailit ) {
      header('Location: '.$referringPage.'?success=true');
      }
      else {
      header('Location: '.$referringPage.'?error=true');
      }
   }";
?>

 

That should allow you to write to a file. You use \ to escape certain characters. So to escape the double quote its \"  as seen above.

here is my full script .. to which i am gettng  that error for line 32

 

 

<?php

$cfname  = isset($_POST['cfname'])  ? trim($_POST['cfname'])  : '';
$cfemail  = isset($_POST['cfemail'])  ? trim($_POST['cfemail'])  : '';
$cfsubject  = isset($_POST['cfsubject'])  ? trim($_POST['cfsubject'])  : '';
$submit = $_POST['submit'];
$flag = $_POST['flag'];
$query = "dbc";
$dolla = "$";



if ( isset($_POST['submit']) & $cfname >= '1' & $cfemail >= '1' & $cfsubject >= '1'   ) { 


$myFile = "../includes/settings2.php";
$fh = fopen($myFile, 'w') or die("can't open file");

$stringData = "<?php

// Change the 4 variables below
$yourName = 'change this to your name';
$yourEmail = 'you@yourdomain.com';
$yourSubject = 'change to your subject line';
$referringPage = 'http://www.yourdomain.com/contact.php';


// No need to edit below unless you really want to. It's using a simple php mail() function. Use your own if you want
function cleanPosUrl ($str) {
return stripslashes($str);
}
   if ( isset($_POST['sendContactEmail']) )
   {
   $to = $yourEmail;
   $subject = $yourSubject.': '.$_POST['posRegard'];
   $message = cleanPosUrl($_POST['posText']);
   $headers = 'From: '.cleanPosUrl($_POST['posName']).' <'.$_POST['posEmail'].\">\r\n\";
   $headers .= 'To: '.$yourName.' <'.$yourEmail.'>'.\"\r\n\";
   $mailit = mail($to,$subject,$message,$headers);
      if ( @$mailit ) {
      header('Location: '.$referringPage.'?success=true');
      }
      else {
      header('Location: '.$referringPage.'?error=true');
      }
   }
?>
";



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

//echo "test the output : yes it worked";

header("Location: step_04.php");

}


?>

 

 

<?php

$cfname  = isset($_POST['cfname'])  ? trim($_POST['cfname'])  : '';
$cfemail  = isset($_POST['cfemail'])  ? trim($_POST['cfemail'])  : '';
$cfsubject  = isset($_POST['cfsubject'])  ? trim($_POST['cfsubject'])  : '';
$submit = $_POST['submit'];
$flag = $_POST['flag'];
$query = "dbc";
$dolla = "$";



if ( isset($_POST['submit']) & $cfname >= '1' & $cfemail >= '1' & $cfsubject >= '1'   ) { 


$myFile = "../includes/settings2.php";
$fh = fopen($myFile, 'w') or die("can't open file");

  ob_start();
?>
// Change the 4 variables below
$yourName = 'change this to your name';
$yourEmail = 'you@yourdomain.com';
$yourSubject = 'change to your subject line';
$referringPage = 'http://www.yourdomain.com/contact.php';


// No need to edit below unless you really want to. It's using a simple php mail() function. Use your own if you want
function cleanPosUrl ($str) {
return stripslashes($str);
}
   if ( isset($_POST['sendContactEmail']) )
   {
   $to = $yourEmail;
   $subject = $yourSubject.': '.$_POST['posRegard'];
   $message = cleanPosUrl($_POST['posText']);
   $headers = 'From: '.cleanPosUrl($_POST['posName']).' <'.$_POST['posEmail'].\">\r\n\";
   $headers .= 'To: '.$yourName.' <'.$yourEmail.'>'.\"\r\n\";
   $mailit = mail($to,$subject,$message,$headers);
      if ( @$mailit ) {
      header('Location: '.$referringPage.'?success=true');
      }
      else {
      header('Location: '.$referringPage.'?error=true');
      }
   }
<?php
  $stringData = "<?php\n".ob_get_clean()."\n?>";

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

//echo "test the output : yes it worked";

header("Location: step_04.php");

}


?>

I forgot that you needed to escape the $ also.

 

$stringData = "<?php

// Change the 4 variables below
$yourName = 'change this to your name';
$yourEmail = 'you@yourdomain.com';
$yourSubject = 'change to your subject line';
$referringPage = 'http://www.yourdomain.com/contact.php';


// No need to edit below unless you really want to. It's using a simple php mail() function. Use your own if you want
function cleanPosUrl ($str) {
return stripslashes($str);
}
   if ( isset(\$_POST['sendContactEmail']) )
   {
   $to = $yourEmail;
   $subject = $yourSubject.': '.\$_POST['posRegard'];
   $message = cleanPosUrl(\$_POST['posText']);
   $headers = 'From: '.cleanPosUrl(\$_POST['posName']).' <'.\$_POST['posEmail'].\">\r\n\";
   $headers .= 'To: '.\$yourName.' <'.\$yourEmail.'>'.\"\r\n\";
   $mailit = mail(\$to,\$subject,\$message,\$headers);
      if ( @\$mailit ) {
      header('Location: '.\$referringPage.'?success=true');
      }
      else {
      header('Location: '.\$referringPage.'?error=true');
      }
   }
?>
";

 

Should work.

 

Or you could just change the ' to double like so:

 

$stringData = '<?php

// Change the 4 variables below
$yourName = "change this to your name";
$yourEmail = "you@yourdomain.com";
$yourSubject = "change to your subject line";
$referringPage = "http://www.yourdomain.com/contact.php";


// No need to edit below unless you really want to. It"s using a simple php mail() function. Use your own if you want
function cleanPosUrl ($str) {
return stripslashes($str);
}
   if ( isset($_POST["sendContactEmail"]) )
   {
   $to = $yourEmail;
   $subject = $yourSubject.": ".$_POST["posRegard"];
   $message = cleanPosUrl($_POST["posText"]);
   $headers = "From: ".cleanPosUrl($_POST["posName"])." <".$_POST["posEmail"].\">\r\n\";
   $headers .= "To: ".$yourName." <".$yourEmail.">".\"\r\n\";
   $mailit = mail($to,$subject,$message,\$headers);
      if ( @$mailit ) {
      header("Location: ".$referringPage."?success=true");
      }
      else {
      header("Location: ".$referringPage."?error=true");
      }
   }
?>
';

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.