• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

file_put_contents() in __destruct() funktioniert nicht...

web673

Neues Mitglied
Hey,

weiss jemand von euch wieso file_put_contents() in der __destruct() Funktion nicht geht?

Code:
class fputsTest
{
	function __construct()
	{
		$filec = @file_get_contents("test.txt");
		echo $filec;
		$this->test();
	}
	function __destruct()
	{
		file_put_contents("test.txt","test content");
	}
	
	function test()
	{
		file_put_contents("test.txt","test content_with test()<br>");
	}
}
$test = new fputsTest();

in der test.txt steht jetzt nur "test content_with test()" und nicht "test content", wie erwünscht.

wenn ich __destruct() aufrufe in der __construct() Funktion oder sonstwo funktioniert es... aber ich will ja z.b ein autospeichern einbauen...

Bitte um Hilfe, danke euch

Edit: meine PHP Version ist 5.3.5 (Windows)
 
Zuletzt bearbeitet:
Werbung:
Note:

Destructors called during the script shutdown have HTTP headers already sent. The working directory in the script shutdown phase can be different with some SAPIs (e.g. Apache).

- PHP: Constructors and Destructors - Manual

Lass dir mal testweise im Destruktor das hier ausgeben:

PHP:
echo '<p>CWD: ' . getcwd() . '</p>';

Lösung ist es vermutlich, den realpath als Instanzvariable zu speichern.

Grob:

PHP:
class fputsTest
{
    protected $path;

	function __construct()
	{
        $this->path = realpath('writeable/test.txt');

		$filec = @file_get_contents($this->path);
		echo $filec;
		$this->test();
	}
	function __destruct()
	{
		file_put_contents($this->path,"test content");
	}

	function test()
	{
		file_put_contents($this->path,"test content_with test()<br>");
	}
}
$test = new fputsTest();
 
Danke dir, funktioniert perfekt.

wusste garnicht das es realpath() gibt...

Die Klasse hat mir meine Dateien wirklich vor das htdocs Verzeichnis geschrieben...
 
Werbung:
Zurück
Oben