Log in

View Full Version : fwrite help



borris83
03-30-2009, 03:59 PM
Hi I am trying to write to a file using fwrite()...

Can I use a variable inside fwrite? If yes then how to do that?

I have tried to use two variables $surl and $lurl but they don't work... Instead of creating a file with the name as the value of the variable, it actually creates a file $surl.php


$fp = fopen('$surl.php','w' );
fwrite($fp, '<?php header("Location: $lurl"); exit; ?>');
fclose($fp);

borris83
03-30-2009, 04:08 PM
Hi, I got the first line working by changing it to the following:

$fp = fopen($surl . '.php','w' );

But the second line doesn't work.. It writes the variable itself instead of writing its value.. Pls help

JasonDFR
03-30-2009, 05:06 PM
You cannot enclose variables in single quotes.


<?php

$lurl = 'http://www.some.com';

$write = '<?php header("Location: ' . $lurl . '"); exit; ?>';

echo $write; // view page source

//fwrite($fp, $write);

exit;

Schmoopy
03-30-2009, 05:12 PM
If you want to you can use curly braces around variables so you don't have to add concatenating operators to the string, but as Jason says, if you put a variable inside single quotes it will end up writing it out literally.


"Hello my name is {$name}";

is the same as:


"Hello my name is " . $name;

Whatever floats your boat.

borris83
03-31-2009, 12:36 AM
Thank you,

This works:

fwrite($fp, '<?php header("Location: ' . $lurl . '"); exit; ?>');