Jump to content

Writing into a file


dalawh

Recommended Posts

file_put_contents is the equivalent of fopen, fwrite, fclose. You can have it either replace the contents or append, and it will create the file if it doesn't exist.
So fopen, fwrite, and fclose is for multiple data. What I mean is that you can use fwrite multiple of times without erasing the previous content. file_put-contents is like a single fwrite, so every time it is used, it erases the previous data, unless you append. Am I right? Which is more convenient, faster, safer, etc? Edited by dalawh
Link to comment
Share on other sites

How do you append using file_put_contents? Does it have to do with it's parameters?
Yes, the third parameter. Check the manual for examples.
file_put-contents is like a single fwrite, so every time it is used, it erases the previous data. Am I right?
It will only erase the data if you don't append, but yes every call to file_put_contents uses fopen, fwrite, and fclose. It doesn't leave the file handle open.
Link to comment
Share on other sites

Seems like file_put_contents is so much better than fwrite if you are using it to keep data in a file. If I use file_get_contents and file_put_contents, how would I check if the file was opened successfully that way I won't get thrown an error? When using fopen, fwrite, fclose, I can use...

$file=fopen($fileName,"a+");   if($file){...} //<-- Check

Edited by dalawh
Link to comment
Share on other sites

Guest So Called
Seems like file_put_contents is so much better than fwrite if you are using it to keep data in a file.
It works best to use fwrite() if you are writing records in several places in your script, adding to a file. If you're going to read in or write out an entire file at one time then file_get_contents() and file_put_contents() work well. It is often the case that fread() and fwrite() get used in iterative loops like while or for.
Link to comment
Share on other sites

You can only check after the fact, file_put_contents will return boolean false if it failed, or else it will return the number of bytes written.
Using file_get_contents and file_put_contents seems to make the code much cleaner and easier to read.
  if(file_exists($fileName)){ //File exists   $prev=file_get_contents($fileName);   if(CONDITION){ //File opens successfully    file_put_contents($fileName,$data,FILE_APPEND); //Writes 'last | first | IP | date and time | page' into firstVisitLog.txt   }  } else{ //File does not exists   if(CONDITION){ //File opens successfully    file_put_contents($fileName,$data); //Writes 'last | first | IP | date and time | page' into firstVisitLog.txt   }  }

I am still unsure of what to fill in for the CONDITION to check whether the file was opened successfully. f I used "if(file_put_contents($fileName, $data)!=FALSE){...}". Wouldn't that make it already write in the file and doing it inside the if statement will cause it to write it again? I tried this and it seems to have given me an error. So this will not work.

It works best to use fwrite() if you are writing records in several places in your script, adding to a file. If you're going to read in or write out an entire file at one time then file_get_contents() and file_put_contents() work well. It is often the case that fread() and fwrite() get used in iterative loops like while or for.
Oh, okay.
Link to comment
Share on other sites

Guest So Called
if ($contents = file_get_contents(....)) {    // do something with $contents hereelse {    // handle the error here}

The function file_get_contents() returns FALSE on error (per the manual). The function file_put_contents() returns number of bytes written or FALSE on error.

Link to comment
Share on other sites

f I used "if(file_put_contents($fileName, $data)!=FALSE){...}". Wouldn't that make it already write in the file and doing it inside the if statement will cause it to write it again?
No, the function only runs once, but you need to use strict comparison to distinguish between a failure and simply writing no data (0 bytes): if (file_put_contents($fileName, $data) !== FALSE) You also don't need to check if the file exists, it will be created if it doesn't.
Link to comment
Share on other sites

No, the function only runs once, but you need to use strict comparison to distinguish between a failure and simply writing no data (0 bytes): if (file_put_contents($fileName, $data) !== FALSE) You also don't need to check if the file exists, it will be created if it doesn't.
I am trying to figure out if it opened successfully. In fwrite, I used "if($file)", but it doesn't work the same way for file_put/get.
Link to comment
Share on other sites

So Called told me it is always good to check. Using fwrite without checking if fopen workd correctly could cause an error when it doesn't open successfully. Basically what I am asking is whether file_put/get will cause errors if the file was not open successfully. I know it will return FALSE, but will I get an error?

Link to comment
Share on other sites

Basically what I am asking is whether file_put/get will cause errors if the file was not open successfully.
I believe it will issue a warning, but I'm not positive. It may just be a notice. But yes, PHP will handle the error message the same way it handles others (logging it, displaying it, whatever your settings are). In a practical sense it doesn't matter if you check for errors when opening, writing, and closing the file versus just checking if the entire process worked. On a production server you should be using error logging for all errors and using a statement like the above to tell your users that the process didn't succeed. You can check the error log at your leisure to find out why and fix it.
Link to comment
Share on other sites

I believe it will issue a warning, but I'm not positive. It may just be a notice. But yes, PHP will handle the error message the same way it handles others (logging it, displaying it, whatever your settings are). In a practical sense it doesn't matter if you check for errors when opening, writing, and closing the file versus just checking if the entire process worked. On a production server you should be using error logging for all errors and using a statement like the above to tell your users that the process didn't succeed. You can check the error log at your leisure to find out why and fix it.
Checking it like I did in fopen won't cause the errors that will mess with the rest of the script. Since I can't check file_put/get and it causes an error, it will potentially mess with the script, which is something I do not want.
  if(file_exists($fileName)){ //File exists   $prev=file_get_contents($fileName);   file_put_contents($fileName,$data,FILE_APPEND); //Writes 'last | first | IP | date and time | page' into firstVisitLog.txt  } else{ //File does not exists   file_put_contents($fileName,$data); //Writes 'last | first | IP | date and time | page' into firstVisitLog.txt  }

Since I am not checking, the above code will work and it seems much simpler than the fwrite version. What do you think? Am I forgetting something important?

Link to comment
Share on other sites

Since I can't check file_put/get and it causes an error, it will potentially mess with the script, which is something I do not want.
I don't understand. You can check. It doesn't need to cause a problem unless you let it. If you want to check, then check the return values of the two functions to see if they succeeded and respond appropriately.
Link to comment
Share on other sites

I don't understand. You can check. It doesn't need to cause a problem unless you let it. If you want to check, then check the return values of the two functions to see if they succeeded and respond appropriately.
How would I check file_put_contents? If I use it, I am already calling the function. What I mean is if I check it with and if statement, wouldn't I be calling it and the contents will be put into the file? If that is the case, won't using the file_put_contents inside the if statement just cause it to put the content in the file again. I already tried to put the file_put_contents inside the if statement and I got an error. Basically I have no idea how to check and I was asking you how I would go about doing it. Edited by dalawh
Link to comment
Share on other sites

Just like I showed:

if (file_put_contents($fileName, $data) !== FALSE){  echo 'succeeded';}else{  echo 'failed';}

And yes, you do not check if it is going to work, you check it if did work. If it didn't work then the user isn't really going to care specifically why it didn't work, but that specific error message will be in your error log so that you can fix the problem.

Link to comment
Share on other sites

Just like I showed:
if (file_put_contents($fileName, $data) !== FALSE){  echo 'succeeded';}else{  echo 'failed';}

And yes, you do not check if it is going to work, you check it if did work. If it didn't work then the user isn't really going to care specifically why it didn't work, but that specific error message will be in your error log so that you can fix the problem.

The echo prints the string onto the page. If you want nothing printed, could I just do the below?
if (file_put_contents($fileName, $data) !== FALSE){ } else{ }

The above is the exact same as the below.

file_put_contents($fileName, $data);

If they are the same, wouldn't using the second one be better? Basically, there is no point of checking since checking or not, it won't do much more than print a statement.

Link to comment
Share on other sites

You can have it do whatever you want to do, printing a string was just an example. If you don't need anything to happen if it fails then yeah, you don't need any additional code. You asked how to check if it failed and I showed you.

Link to comment
Share on other sites

You can have it do whatever you want to do, printing a string was just an example. If you don't need anything to happen if it fails then yeah, you don't need any additional code. You asked how to check if it failed and I showed you.
Oh okay. Thanks for the help.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...