Michael Beckmann
Michael Beckmann

Reputation: 11

PHP isn't able to read file

I'm stuck trying to open a file with fopen in php.

        $db_ausgaenge = "statuseing.php";
        $dout = fopen($db_ausgaenge, "x+");
        print $dout;
        print(shell_exec('whoami'));
        print(shell_exec('pwd'));
        print(shell_exec('id'));
        fwrite($dout, $out);
        fclose($dout);

Warning: fopen(statuseing.php): failed to open stream: File exists in /var/www/html/zufallsgenerator.php on line 33

I checked following items:

After daemon-reload and restarting apache2 via systemctl nothing changed, phpinfo() didn't show the path given in the config. restarting the system via init 6 didn't took effect, too.

Upvotes: 0

Views: 151

Answers (3)

Rob G
Rob G

Reputation: 612

statuseing.php already exists.

See the manual (http://php.net/manual/en/function.fopen.php) - opening in x or x+ mode says: Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE

Upvotes: 1

num8er
num8er

Reputation: 19372

Try this:

    $db_ausgaenge = __DIR__."/statuseing.php";
    $dout = fopen($db_ausgaenge, "a+"); // x+ will throw error cuz it tries to open existing file, thx to: bluegman991  (;
    print(shell_exec('whoami'));
    print(shell_exec('pwd'));
    print(shell_exec('id'));
    fwrite($dout, $out);
    fclose($dout);

or if You want to truncate file before adding data use w+:

    $db_ausgaenge = __DIR__."/statuseing.php";
    $dout = fopen($db_ausgaenge, "w+");
    print(shell_exec('whoami'));
    print(shell_exec('pwd'));
    print(shell_exec('id'));
    fwrite($dout, $out);
    fclose($dout);

also do some checkups:

1) check free space: df -h
2) check if You can edit that file: nano /var/www/html/statuseing.php

Upvotes: 0

bluegman991
bluegman991

Reputation: 727

Look at the mode you are using.

x+ means that if the file already exists an error will be thrown.

To find the correct mode depending on your scenario check out http://php.net/manual/en/function.fopen.php

Upvotes: 1

Related Questions