Php file writing

  • ARG! this code is a WTF,

    $base64url = $GET["urlaccess"];
    $base64 = strtr($base64url, '-
    ', '+/');
    $base64txt = base64_decode($base64);
    $plaintext = $base64txt;

    $filename = 'access.txt';
    $somecontent = $plaintext;
    echo "$plaintext<br>$base64url<br>$base64";
    echo "woot54";
    fopen($filename, 'a+');
    fwrite($handle, $somecontent);
    echo "woot2";
    echo "woot4";

     FYI:i added those echo's for debugging...... so at lest the script would  output something...

    it decodes the base64 well, just the only error i get is

    Warning: fwrite(): supplied argument is not a valid stream resource in /path/to/logging.php on line 13

  • Read the error message. The supplied argument (which would be $handle) is not a valid stream resource. fopen() conveniently returns a stream resource, which would be ideally assigned to a variable called $handle if you wanted to make this work.

  • Change

    fopen($filename, 'a+');


    $handle = fopen($filename, 'a+');

    Anyway You'd want to add some code like:

    if(!$handle) die("Failed to open the file");

    course, I don't mean add THIS code, because causing a script to die()
    is immoral and evil, and should be used only for debugging or if
    something REALLY screws up. Just implement something to report an error
    if fopen fails.)

    There are also redundant variable assignments, ie. you can use $base64txt instead of $plaintext and $somecontent (this will also make this snippet more readable).

Log in to reply

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.