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.
HTTP does not maintain a continuous network connection between request/response cycles, so it would be extremely poor practice to attempt to fopen() a file once and expect it to still be there for a later request. Always fopen/fclose within the request!
I didn't quite understand the rest of it, but you are using a relative pathname for your file and that, also, is not recommended practice for a web application, since there's no real concept of a "current directory" in webapps.
Science is the process of replacing what we "know" with what is TRUE. Politics, alas, often prefers to be the opposite.
This is my favorite show. And this is my favorite tiny ad: