Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

problem with fopen and fwrite


ghost's Avatar
0 0
$name = $_POST['name'];
$content = $_POST['content'];
$url = $_POST['url'];
$ext = $_POST['ext'];
$fullurl = $url."".$ext;
$ourFileName = $fullurl;
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
$myFile = $fullurl;
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $content;
fwrite($fh, $stringData);
fclose($fh);
?>```

When ever the value of $content contains php the file contains the php in plaintext :( like   echo (\"hello world\");

erm ....
Help me please!!
ty
Dotti.

ghost's Avatar
0 0

or these errors

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in C:\xampp\htdocs\functions\test.php on line 2

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in C:\xampp\htdocs\functions\test.php on line 2

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in C:\xampp\htdocs\functions\test.php on line 2

Parse error: syntax error, unexpected $end in C:\xampp\htdocs\functions\test.php on line 3


ghost's Avatar
0 0

djdotti wrote:

$name = $_POST['name'];
$content = $_POST['content'];
$url = $_POST['url'];
$ext = $_POST['ext'];
$fullurl = $url."".$ext;
$ourFileName = $fullurl;
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
$myFile = $fullurl;
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $content;
fwrite($fh, $stringData);
fclose($fh);
?>```

When ever the value of $content contains php the file contains the php in plaintext :( like   echo (\"hello world\");

erm ....
Help me please!!
ty
Dotti.
 
Couple things: disable the smilies so it's easier to read. Second, what are you trying to achieve? Because it seems that the file from which you're reading from is a variable so it will be difficult to help you without more information. You should probably also post the HTML form you're using :)
 
 
Secondly, it would probably be beneficial to use <?php instead of <? at the start of your code :)

ghost's Avatar
0 0

newfile.htm :


<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
</head>

<body>

<form method="POST" action="functions/newfile.php">
	<p align="center">Filename :<br><input type="text" name="name" size="30"></p>
	<p align="center">Url :<br><input type="text" name="url" size="30"><select size="1" name="ext">
	<option value=".php">.php</option>
	<option value=".htm">.htm</option>
	<option>.html</option>
	<option value=".txt">.txt</option>
	</select></p>
	<p align="center">Content :<br><textarea rows="10" name="content" cols="90"></textarea></p>
	<p align="center"><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>

</html>```
newfile.php :
```markup<?
$name = $_POST['name'];
$content = $_POST['content'];
$url = $_POST['url'];
$ext = $_POST['ext'];
$fullurl = $url."".$ext;
$ourFileName = $fullurl;
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
$myFile = $fullurl;
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $content;
fwrite($fh, $stringData);
fclose($fh);
?>```

ghost's Avatar
0 0

if I understand correctly, it looks like you need to use stripslashes(), I think you'd put it here markupfwrite($fh, stripslashes($stringData));


ghost's Avatar
0 0

Again, what are you trying to achieve excactly? We can guess around all day but it won't help you much :|

(I mean I made the files, uploaded it to my server and I'm still kind of puzzled as to the objective).


ghost's Avatar
0 0

xtrmsk8r91 wrote: if I understand correctly, it looks like you need to use stripslashes(), I think you'd put it here markupfwrite($fh, stripslashes($stringData));

:o:O:O:O:O:O:O: 00OH ty sk8r! You rock … I owe ya ;)


ghost's Avatar
0 0

lol no problem, glad it worked :)


ghost's Avatar
0 0

if all you are doing is opening up files, just use

read:

file_get_contents("file.txt");

write:

file_put_contents("file.txt",$contents);

kills the whole opening file handles deal. one nice command to do it all for ya.


ghost's Avatar
0 0

im writing data to a file … But sk8r has sorted it now thankyou Dotti.