ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

09 – PHP-Accessing File

In this tutorial, we’re going to access file in PHP. And primarily we’re going to focus on this lesson in dealing with  fopen (filename, mode) so we’re going to open a file and we’re to provide a filename and we’re going to tell it what mode is should open it. And remember using “fopen” opens up a file whether it exists or not so we will create it if necessary.

At this time, we will know first how to deal with FILE ACCESS MODES:
Below are the three basic ways to open a file and the corresponding character that PHP uses. This is used to specify the intentions when you open a file.
r – it reads from the start of a file, this is just reading and it the file must exist if we’re going to read it.
W – it Truncate or Write from start, and it erases anything what is in the file that are already existing or it creates a new file if didn’t.

A – it is used to append or write to the end and these can be useful especially when we are doing a log file and we just want to add one new entry and then exit the file.

And using these modes offers an alternative version used for Reading and Writing into a file. The combination is done by placing a plus sign “+” after the file mode character.
Read/Write: r+ – it opens a file that it can be read from and written to.
Write/Read: w+ – it’s going to truncate and put it at the start but then we can go ahead and write it there.
Append: a+ – it puts us at the end of the file and also enable us to read write to the file.

At this time we’re going to create a new PHP file called “accessFile.php” and add the following code.

  1. <?php
  2. //specify the filename and we call it as hello.txt
  3. $testfile = 'hello.txt';
  4. //and inside the if statement, we pass the $testfile variable to fopen and we specify the mode as write
  5. //and fopen return a handle as a value, and using handle is useful because we could close it
  6. if($fh = fopen($testfile, 'w')){
  7.     //close the handle
  8.     fclose($fh);
  9. }else{
  10.     //if the file can't be open..and it can prevent some error might happen
  11.     echo 'Could not open the file';
  12. }
  13.  
  14. ?>

After executing the code above, as expected it will create a new file in our document root. And it looks like as shown below.
a1

Facebook Comments

Check Your Domain Ranking

6 Comments

Add a Comment
  1. It is unique as it is the very first real-time, live-market mobile trading
    game in the world. The leveling kits are what you
    need to even out the stance of your truck. You can find a great
    number of online games that you can try.

  2. Hello there, You’ve done an excellent job. I will definitely digg it and personally recommend to my friends.
    I’m sure they’ll be benefited from this website.

  3. Accionador coaxial ρara persianas Accionador electromecánico ɗe
    brazo para puertas batientes Cerrajero la capital española JR – Amaestramientos Ꮮоs cuartos trasteros normalmente acostumbran а
    estar ubicadas en zonas de mᥙү ρoco tránsito a
    determinadas ɦoras del día. https://crownstream8.wordpress.com/2016/07/15/%ef%bb%bfcerrajeros-cabrils-telf-635-343-388-%e3%80%90-economicos-%e3%80%91/

  4. Hi, this weekend is pleasant for me, for the reason that this moment i am reading this impressive informative post here at my
    home.

  5. Magnificent beat ! I would like to apprentice at the same time as you amend your site, how could i subscribe
    for a weblog web site? The account helped me a appropriate deal.
    I had been a little bit familiar of this your broadcast offered brilliant transparent
    concept

  6. magnificent points altogether, you simply won a logo new reader.
    What may you suggest about your post that you just made some days in the past?

    Any sure?

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!