Web Hosting Talk







View Full Version : cleanfile() not working?


UrlGuy
05-07-2005, 06:20 PM
Hi,

I want to clear the content of a html logfile, this logfile resides in a unreadable directory behind public_html so the public cant view the logfile. Locating that directory is what the $self variable do.

When I run this script it gives me 404 - page not found.

Anyone got any idea whats wrong here?

<?php
$self=explode("public_html/",$_SERVER['SCRIPT_FILENAME']);
function cleanfile(file=$self[0]."failed.html", content="\n") {
handle = fopen($file,'w');
fwrite($handle, $content);
fclose($handle);
}
?>

t3r0
05-08-2005, 04:55 PM
Hi,

First of all 404 really means that the reguested page is NOT found, so thats not a php error.

where are you calling the function cleanfile()? is that the whole file? if so you must call the function to run it.

Example:

<?php

function cleanfile($file = false, $content="\n") {
if ($file && is_file($file)) {
handle = fopen($file,'w');
fwrite($handle, $content);
fclose($handle);
return true;
} else {
return false;
}
}

$self = explode("public_html/",$_SERVER['SCRIPT_FILENAME']);
$self .= "failed.html"

// Call the function here
if ( cleanfile($self) ) { echo "OK!" } else { echo "ERROR!"}



?>

UrlGuy
05-08-2005, 05:53 PM
Hey thanks man.. I thought the 404 error meant that the file to be 'cleaned' wasnt found and then outputted a not found error.
I am really a newb beginner to PHP so I cant figure out most myself .. yet =(

Thanks for reply man..was starting to think noone would reply hehe...


Although, the code gives me some error:

Parse error: parse error, unexpected '=' in /home/user/public_html/cleanpls.php on line 5



I added a $ infront of the handle on line 5, but then gives me this error:

Parse error: parse error, unexpected T_IF in /home/user/public_html/cleanpls.php on line 18



Anyone got any idea?

All help greatly aprecciated :]

orbitz
05-08-2005, 08:07 PM
why don't you post your codes up....
i am lazy at guessing.

UrlGuy
05-08-2005, 08:11 PM
I did at top.. thats all the code I use heheh..

and t3r0's code which gave me error :\

I thought this was a builtin php function but o'well

orbitz
05-08-2005, 08:15 PM
look at this line, there're 2 errors of the same type

if ( cleanfile($self) ) { echo "OK!" } else { echo "ERROR!"}


and this:

$self .= "failed.html"

ps: for php code, use the bbcode style for php

UrlGuy
05-08-2005, 08:22 PM
Not all sure what you meant there and I dont know what bbcode is either.. I think.. yea im newb :\

$self=explode("public_html/",$_SERVER['SCRIPT_FILENAME']);

is what I used to get the directory behind public html

So if I would do

echo "$self[0].";

it would output: /home/user/

and thats the location of "failed.html" which I want to remove all the content from, it resides at /home/user/failed.html/public_html/

But using

function cleanfile(file=$self[0]."failed.html", content="\n") {

OR

function cleanfile(file="$self[0].failed.html", content="\n") {


both gives me 404 not found :S

What I thought was that the above code should wwas locating failed.html, then clearing content.. but somehow it doesnt find the file at all.. or me just being dumb.. :S

t3r0
05-08-2005, 09:27 PM
Hi,

Sorry i had few typos there in my code :rolleyes: i've been coding so long with Ruby that i always forget ; and $ :D


ok but lets try again... :P

assuming your log file is in "/home/user/failed.html"


<?php
function cleanfile($file = false, $content="\n") {
if ($file && is_file($file)) {
$handle = fopen($file,'w');
fwrite($handle, $content);
fclose($handle);
return true;
} else {
return false;
}
}

// change the correct location of the file here!
$path = "/home/user/failed.html";

// Call the function here
if ( cleanfile($path) ) {
echo "OK!";
} else {
echo "ERROR!";
}

?>


But remember if your php is configured with open base dir protection on, your scripts cannot write to any directory above the directory that contains the PHP script.