posted 3 years ago
Hello!
I am new to php, and I am trying to learn it.
Here is a code that I have been trying to make to work:
In this practice, I am trying to make a simple webpage that creates a file, and deletes it afterwards.
You see, this code works, but not as I would like it to:
First of all, once I load the page and press on the button that creates the new text file containing a message, I actually need to press on it a second time before it decides to even change the page to the next one. And you see, the exact same thing happens then I decide to delete that file. Of course it is calling that same page (called "practice2.php"), from which a different outcome would be resulted depending on if that text file exists or not.
Secondly, from the looks of it, I cannot manage to save the name of the file that has to be created and deleted, as apparently it is considered as "fopen('', 'w+')" instead of "fopen('messages.txt','w+')", of course error message displayed in "/Applications/XAMPP/xamppfiles/logs/php_error_log". And, the same thing would happen when I decide to unlink that file.
Finally, as you can see in the code, I have used "$file = fopen('messages.txt', 'w+');", line 28, inside of a function. Is there a way to declare outside of it, without having the page doing the "fopen" action when loading or reloading the page? I feel that this will become an issue if I would want to have different functions that would need to call this.
Could someone please help me correct my mistakes?
Please bear with me, i'm trying to learn php.