Need some PHP help for a tool.

ThePayRoll

BANNED
Joined
Dec 1, 2010
Messages
230
Reaction score
35
The problem is, I found this code on my machine from awhile ago, I don't know jack-shit worth of PHP, the problem is, when I post anything into the textbox ex:
Code:
 <iframe src="http://google.com"></iframe>

this is what shows up in the file I create:
Code:
<iframe src=\"http://google.com\"></iframe>

Does anyone know how to fix this? Basically just to remove the backslashes \

Here's the PHP code:
Code:
<?PHP
 
if(isset($_POST['filename']))
{
	$filename = $_POST['filename'];
	$text = $_POST['text'];

	$filename = "files/$filename"; #Must CHMOD to 666
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
if(isset($_POST['filename']))
{
	$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
	if ($fp) {
		fwrite ($fp, $text);
		fclose ($fp);
		echo ("File written");
	}
	else {
		echo ("File was not written");
	}
}else{
?>
<form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
filename: <input name="filename" type="text" size="20">
<BR>
content:
<BR>
<TEXTAREA NAME="text" COLS=40 ROWS=6></TEXTAREA>
<BR>
<input type="submit" name="submit" value="save file">
</form>
<?php } ?>
</body>
</html>

I know I can download the file I create and just remove the \ but that totally defeat's the purpose.

Anyone experienced in PHP know or have any idea on how to fix this?
 
Thank you both sooooooo much!
I appreciate it so very much!
 
You can also just use preg_replace;

See my good below, its should work.. test it & Enjoy (don't write Thank you.. Click on Thanks :) )
PHP:
<?PHP
 
if(isset($_POST['filename']))
{
	$filename = $_POST['filename'];
	$text = $_POST['text'];

	$filename = "files/$filename"; #Must CHMOD to 666
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
if(isset($_POST['filename']))
{
	$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
	if ($fp) {
		fwrite ($fp, $text);
		fclose ($fp);
		echo ("File written");
	$file = $filename;
	$fh = fopen($file, 'r+');
    $contents = fread($fh, filesize($file));
	$new_contents = preg_replace('/\\\\/','',$contents);
	$fh = fopen($file, 'r+');
	fwrite($fh, $new_contents);
	fclose($fh);
	}
	else {
		echo ("File was not written");
	}
}else{
?>
<form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
filename: <input name="filename" type="text" size="20">
<BR>
content:
<BR>
<TEXTAREA NAME="text" COLS=40 ROWS=6></TEXTAREA>
<BR>
<input type="submit" name="submit" value="save file">
</form>
<?php } ?>
</body>
</html>
 
You can also just use preg_replace;

See my good below, its should work.. test it & Enjoy (don't write Thank you.. Click on Thanks :) )
PHP:
<?PHP
 
if(isset($_POST['filename']))
{
	$filename = $_POST['filename'];
	$text = $_POST['text'];

	$filename = "files/$filename"; #Must CHMOD to 666
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
if(isset($_POST['filename']))
{
	$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
	if ($fp) {
		fwrite ($fp, $text);
		fclose ($fp);
		echo ("File written");
	$file = $filename;
	$fh = fopen($file, 'r+');
    $contents = fread($fh, filesize($file));
	$new_contents = preg_replace('/\\\\/','',$contents);
	$fh = fopen($file, 'r+');
	fwrite($fh, $new_contents);
	fclose($fh);
	}
	else {
		echo ("File was not written");
	}
}else{
?>
<form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
filename: <input name="filename" type="text" size="20">
<BR>
content:
<BR>
<TEXTAREA NAME="text" COLS=40 ROWS=6></TEXTAREA>
<BR>
<input type="submit" name="submit" value="save file">
</form>
<?php } ?>
</body>
</html>

But the thing is, I would need it to allow "/" but not have "\" and whenever I would put
Code:
<iframe src="http://google.com"></iframe>
wouldn't it replace the "/' ?
 
But the thing is, I would need it to allow "/" but not have "\" and whenever I would put
Code:
<iframe src="http://google.com"></iframe>
wouldn't it replace the "/' ?

No. In PHP the back slash (\) is a way of escaping something, say you have a variable

Code:
$string = "This is a \"quote\" from my favorite book.";

It will still allow the "QUOTE" but wont actually show the slashes.

In your case the slashes are added so it wont think it's the end of your variable and shoot an error. check out http://php.net/stripslashes
 
Your version of php is apparently configured to use magic_quotes_gpc, which escapes all single-quote, double-quote, backslash, and NULL characters that appear in any HTTP request (GET, POST, or COOKIE.)

The preferred solution is to call stripslashes() only if slashes might have been added because of magic_quotes. This can be done by checking if magic_quotes is on.

Code:
<?php

if (get_magic_quotes_gpc())
  $text = stripslashes($_POST['text']);
else
  $text = $_POST['text'];


if(isset($_POST['filename']))
{
	$filename = $_POST['filename'];
	$filename = "files/$filename"; #Must CHMOD to 666
}

...
 
Back
Top