Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2005
    Location
    Norway
    Posts
    391

    cleanfile() not working?

    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);
    }
    ?>

  2. #2
    Join Date
    Nov 2002
    Location
    Finland
    Posts
    96
    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 Code:
    <?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!"}



    ?>

  3. #3
    Join Date
    Feb 2005
    Location
    Norway
    Posts
    391
    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 :]

  4. #4
    Join Date
    Mar 2004
    Posts
    1,301
    why don't you post your codes up....
    i am lazy at guessing.

  5. #5
    Join Date
    Feb 2005
    Location
    Norway
    Posts
    391
    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

  6. #6
    Join Date
    Mar 2004
    Posts
    1,301
    look at this line, there're 2 errors of the same type
    PHP Code:
    if ( cleanfile($self) ) { echo "OK!" } else { echo "ERROR!"
    and this:

    PHP Code:
    $self .= "failed.html" 
    ps: for php code, use the bbcode style for php

  7. #7
    Join Date
    Feb 2005
    Location
    Norway
    Posts
    391
    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

    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..

  8. #8
    Join Date
    Nov 2002
    Location
    Finland
    Posts
    96
    Hi,

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


    ok but lets try again...

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

    PHP Code:
    <?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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •