silkfire Posted April 2, 2012 Share Posted April 2, 2012 I have a class and the last thing I want it to do is to generate a file with all the data. Unfortunately my write call never seems to run, are there any restrictions on this maybe? If I call file_put_contents somewhere else in the code it works as usual. function __destruct() { echo 'destruct'; // Called without problems file_put_contents('t.txt', 'eeee'); // NEVER PERFORMED, why?? } $b = new Class // doesn't matter its name file_put_contents('t.txt', 'eeee'); // Works perfectly Quote Link to comment https://forums.phpfreaks.com/topic/260217-writing-file-in-destructor-not-working/ Share on other sites More sharing options...
requinix Posted April 2, 2012 Share Posted April 2, 2012 At the end of a script, when PHP closes handles and destroys any remaining objects, the working directory may (and probably will be) completely different from where it was during the rest of the script. That means "t.txt" won't be where you think it should be. Try looking in the main Apache directory. Don't use the destructor to write files. Do it normally: make a separate function for it and call that from your script when you want it to save the file. Quote Link to comment https://forums.phpfreaks.com/topic/260217-writing-file-in-destructor-not-working/#findComment-1333751 Share on other sites More sharing options...
silkfire Posted April 2, 2012 Author Share Posted April 2, 2012 Mmmkay but I want this to be the last thing that is performed, I don't want the user to necessary call a ->finalize() function. Hmmmm.... Quote Link to comment https://forums.phpfreaks.com/topic/260217-writing-file-in-destructor-not-working/#findComment-1333753 Share on other sites More sharing options...
silkfire Posted April 2, 2012 Author Share Posted April 2, 2012 Thank you man I solved it by storing the getcwd() during contructor of class then appending it to the file name during destruction. You were right, the file ended up in the root directory, really weird. Quote Link to comment https://forums.phpfreaks.com/topic/260217-writing-file-in-destructor-not-working/#findComment-1333755 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.