• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

fopen \ unlink learning

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 28102
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Fire me boy! Cool, soothing, shameless self promotion:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic