Jump to content

fopen?


eduard

Recommended Posts

Most of the time file() or file_get_contents() is easier. file() returns an array. file_get_contents() returns a string.
Thanks!However, I don´t understand much of your reply?
Link to comment
Share on other sites

Is your question about how to use fopen? The fopen function is one way to open a file so that you can read the data in the file. Dad mentioned two other ways to do the same that are less complex than fopen, fread, and fclose.

Link to comment
Share on other sites

Is your question about how to use fopen? The fopen function is one way to open a file so that you can read the data in the file. Dad mentioned two other ways to do the same that are less complex than fopen, fread, and fclose.
I have understood that fopen is to open a file but why? Don´t you always write to open (and close) it?
Link to comment
Share on other sites

If PHP needs to read data from a file, like if you upload a spreadsheet and want PHP to process the data, then PHP needs to open the file before it can read the data. Similarly, if PHP is writing data to a file, like you're generating an RTF document and want to save it, PHP needs to open the file before it can write the data.This isn't the programmer opening a PHP source code file and editing the code. This is PHP opening a file for input or output.

Link to comment
Share on other sites

If PHP needs to read data from a file, like if you upload a spreadsheet and want PHP to process the data, then PHP needs to open the file before it can read the data. Similarly, if PHP is writing data to a file, like you're generating an RTF document and want to save it, PHP needs to open the file before it can write the data.This isn't the programmer opening a PHP source code file and editing the code. This is PHP opening a file for input or output.
So, fopen is for the server?
Link to comment
Share on other sites

I don´t understand the function (PHP file): fopenDon´t you always to open the PHP function?
Think of fopen() as a PHP command that allows you to use a specific set of other PHP commands that you would not otherwise be able to use. After you have used these commands always remember to follow with fclose().What are the commands that depend on fopen() and fclose() for their use? You will always be told, when you try to use the command.When you use these other commands you will always have to pass through a special door to get to the file or folder that you wish to manipulate. This special door is called a resource. The fopen() command returns a handle to the door (resource). The fclose() command deletes the handle when you are finished with the resource (the door). The resource (door) gives you special access to the file or folder that you would otherside not have.You can have many handles to the same door (resource) by the way, but each handle is unique and each handle must be deleted after use. All of the special commands can use the same handle.The other commands that depend on fopen() and fclose() do not refer directly to the file or folder that they manipulate, they refer only to the handle. This is because, the handle always refers to a specific door, that always refers to a specific file or folder.It is really very simple, if you keep in mind that there are a specific set of commands that depend on the resource (door) for their use with the file or folder that you wish to manipulate.Roddy
Link to comment
Share on other sites

I have understood that fopen is to open a file but why? Don´t you always write to open (and close) it?
You have it backwards... you always open a file to write to it and then close it... or to be more precise, you need to open a file in order to write to it.When you say "you" in this context, I'm starting to think you aren't getting the idea of programming in general... programming something (e.g. a PHP file) allows you to make the computer do something that you can otherwise do yourself. Yes, YOU (the person) can open a file, write to it and close it... that's what you've been doing when learning HTML. Imagine you had to open 100 files and write the same thing in all of them... tedious, right? Don't want to do it? I don't blame you... however, if you really had to find a way to do it for whatever reason, you can create a program in PHP that does this for you. How do you tell PHP what to do? One step at a time... you tell PHP "open this file, write this, close it, now open this other one, write this, close it" etc... but you can tell it in a more generic way too, like "go over every file in this folder, open each, write something to it, close it".YOU (the person) writing fopen() in your PHP code is like you telling PHP "open this file". The rest of the stuff described above have their own functions, like fclose() for saying "close this file".
Link to comment
Share on other sites

You have it backwards... you always open a file to write to it and then close it... or to be more precise, you need to open a file in order to write to it.When you say "you" in this context, I'm starting to think you aren't getting the idea of programming in general... programming something (e.g. a PHP file) allows you to make the computer do something that you can otherwise do yourself. Yes, YOU (the person) can open a file, write to it and close it... that's what you've been doing when learning HTML. Imagine you had to open 100 files and write the same thing in all of them... tedious, right? Don't want to do it? I don't blame you... however, if you really had to find a way to do it for whatever reason, you can create a program in PHP that does this for you. How do you tell PHP what to do? One step at a time... you tell PHP "open this file, write this, close it, now open this other one, write this, close it" etc... but you can tell it in a more generic way too, like "go over every file in this folder, open each, write something to it, close it".YOU (the person) writing fopen() in your PHP code is like you telling PHP "open this file". The rest of the stuff described above have their own functions, like fclose() for saying "close this file".
Thanks!
Link to comment
Share on other sites

Think of fopen() as a PHP command that allows you to use a specific set of other PHP commands that you would not otherwise be able to use. After you have used these commands always remember to follow with fclose().What are the commands that depend on fopen() and fclose() for their use? You will always be told, when you try to use the command.When you use these other commands you will always have to pass through a special door to get to the file or folder that you wish to manipulate. This special door is called a resource. The fopen() command returns a handle to the door (resource). The fclose() command deletes the handle when you are finished with the resource (the door). The resource (door) gives you special access to the file or folder that you would otherside not have.You can have many handles to the same door (resource) by the way, but each handle is unique and each handle must be deleted after use. All of the special commands can use the same handle.The other commands that depend on fopen() and fclose() do not refer directly to the file or folder that they manipulate, they refer only to the handle. This is because, the handle always refers to a specific door, that always refers to a specific file or folder.It is really very simple, if you keep in mind that there are a specific set of commands that depend on the resource (door) for their use with the file or folder that you wish to manipulate.Roddy
Thanks!
Link to comment
Share on other sites

Yes. All the functions and files we have been discussing exist on your server. PHP can download files to a user's browser, but the user's own operating system will do the writing.
So, who write the programs for the server? Persons who made e. g. PHP?
Link to comment
Share on other sites

Define "programs for the server".PHP is a program running on the server... it takes your PHP files as input and gives you whatever output you've requested in your files. THAT program is written by the authors of the PHP language.Your PHP files themselves are essentially also programs, and they run on the server (thru PHP), making them "programs for the server" too. THOSE programs are written by you.

Link to comment
Share on other sites

The "PHP tools" originally stood for "personal home page tools". It was a way for people to do things like put a counter on their page, or process a form, or read data from other files and put it on the web page, etc. Later they added support for databases and other advanced things. They've re-written the PHP language several times, now they say it just stands for "PHP hypertext pre-processor". So yeah, when PHP was first created it was created to fill a need for open-source tools to make your pages more dynamic.

Link to comment
Share on other sites

Thanks! But why have they developed PHP? There was a demeand, it didn´t exist, etc.?
Normally, writing programs requires that you write your code, pass it to a program which translates it into something the computer can execute directly (this step is known as "compiling"), and then set up the program to execute when expected and making everything else ready for it (this step is known as "installing").This means that any change in your code requires basically a recompilation and reinstallation of the whole program (well... not exactly, but pretty much...).With PHP, you can just install PHP once, and then pass it your PHP files. After that, you can change your PHP file, and once you give it again to PHP, PHP will do the new thing... no need to be compiling and setting up anything.
Link to comment
Share on other sites

Normally, writing programs requires that you write your code, pass it to a program which translates it into something the computer can execute directly (this step is known as "compiling"), and then set up the program to execute when expected and making everything else ready for it (this step is known as "installing").This means that any change in your code requires basically a recompilation and reinstallation of the whole program (well... not exactly, but pretty much...).With PHP, you can just install PHP once, and then pass it your PHP files. After that, you can change your PHP file, and once you give it again to PHP, PHP will do the new thing... no need to be compiling and setting up anything.
Thank you very much for your replies?
Link to comment
Share on other sites

Think of fopen() as a PHP command that allows you to use a specific set of other PHP commands that you would not otherwise be able to use. After you have used these commands always remember to follow with fclose().What are the commands that depend on fopen() and fclose() for their use? You will always be told, when you try to use the command.When you use these other commands you will always have to pass through a special door to get to the file or folder that you wish to manipulate. This special door is called a resource. The fopen() command returns a handle to the door (resource). The fclose() command deletes the handle when you are finished with the resource (the door). The resource (door) gives you special access to the file or folder that you would otherside not have.You can have many handles to the same door (resource) by the way, but each handle is unique and each handle must be deleted after use. All of the special commands can use the same handle.The other commands that depend on fopen() and fclose() do not refer directly to the file or folder that they manipulate, they refer only to the handle. This is because, the handle always refers to a specific door, that always refers to a specific file or folder.It is really very simple, if you keep in mind that there are a specific set of commands that depend on the resource (door) for their use with the file or folder that you wish to manipulate.Roddy
Thanks!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...