Jump to content

[SOLVED] File handling Questions!


pkirsch

Recommended Posts

Hi,

I have a few questions regarding File Handling!

If you use fopen to open a file, and the file doesn't exist, it will create it. Right?

What happens if you never close a file using fclose and just leave it open?

Link to comment
Share on other sites

If you open a file in write mode

 

<?php
$fp=fopen('empty.txt', 'w');
fclose($fp);                          // creates new empty file  (no fwrite required)
?>

 

it does not need to exist. If it doesn't exist the new file is create, if it does exist then the existing file is overwritten.

Link to comment
Share on other sites

Oops, sorry I should have looked at the manual first.

 

You should do a check first though:

<?php
$filename = '/path/to/foo.txt';
$fp=fopen($filename, 'w');
if(file_exists($filename)){
     fclose($fp);                          // creates new empty file  (no fwrite required)
}else{
     echo 'Flie Doesn\'t Exist.';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.