954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

cannot write file

[code] target.php:

xas xjahs dajdh ajdhjas da d xxxx na dh ada hd

[code] main.php:
<?php
// removal malicious script by forzadraco

$filename="target.php";

$existfile=@fopen($filename,"w");

if($existfile){
echo "file berhasil dibaca \n\n";
}else{
echo "file gagal dibaca \n\n";
}


if( false == ($str=file_get_contents( $filename )))
echo "Could not read file.";
else
echo "File contents: ".htmlspecialchars($str);

$hsl=preg_replace("/xxxx/i","draco",$str);

echo "".htmlspecialchars($hsl);

fwrite($existfile,$hsl);
fclose($existfile);

?>

why is not work ? and why target.php become empty file..?

thanks

forzadraco
Junior Poster in Training
70 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 
<?php
// removal malicious script by forzadraco

$filename="target.php";

$existfile=fopen($filename,"a+");

if($existfile){
echo "file berhasil dibaca \n\n";
}else{
echo "file gagal dibaca \n\n";
}


if( false == ($str=file_get_contents( $filename )))
echo "Could not read file.";
else
echo "File contents: ".htmlspecialchars($str);

$hsl=preg_replace("/xxxx/i","draco",$str);

echo "<hr />".htmlspecialchars($hsl);

fwrite($existfile,$hsl);
fclose($existfile);

?>

Check "mode" http://in2.php.net/manual/en/function.fopen.php . Mode "w" : Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

I have read the script and if you are using php5 then the following might be better with the functions file_get_contents and file_put_contents.

<?php
// removal malicious script by forzadraco

$filename="target.php";

$existfile=file_get_contents($filename);

if($existfile){
echo "file berhasil dibaca \n\n";
}else{
echo "file gagal dibaca \n\n";
}


if( false == ($str=file_get_contents( $filename ))) {
echo "Could not read file.";
} else {
echo "File contents: ".htmlspecialchars($str);
}

$hsl=preg_replace("/xxxx/i","draco",$str);

echo "<hr />".htmlspecialchars($hsl);

file_put_contents($hsl,$existfile);

?>

Note: It's good to see you made an attempt at adding the opening code tags but next time try to add the closing code tags too.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You