Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Basic PHP File Handling – Create Read Write Append Copy Move Delete

INTRODUCTION
PHP FILE YOGA

Welcome to a beginner’s tutorial on PHP file handling. Need to deal with some files in your latest project? You have come to the right place, and we will walk through the basic files yoga with examples, step-by-step:

  • How to create a new file.
  • Read an existing file.
  • Write and append data to an existing file.
  • Copy and move files.
  • Rename a file.
  • Finally, how to delete a file.

Read on to find out!

I have included a zip file with all the example source Code at the start of the tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

CONFESSION
AN HONEST DISCLOSURE

Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are evil affiliate links and advertisements throughout this website. Whenever you buy things from these dreaded links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!

NAVIGATION
TABLE OF CONTENTS

Section A
Example Code Download

Section B
File Basics

Section C
Useful Bits & Links

Closing
What’s Next?

SECTION A
EXAMPLE CODE DOWNLOAD

First, here is the download link to the example source code as promised.

SOURCE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

QUICK START

  • Download and unzip into a folder.
  • Hooray! There is no database involved, so just follow through each of the files contained within.

SECTION B
FILE BASICS

Moving on, let us start with the PHP file yoga session.

1. CREATING FILES

1-create.php
  • Two of the most important file functions in PHP are fopen and fclose.
  • fopen(FILE, MODE) will open a specified file, and there are several read/write modes – I shall leave a link to the official PHP manual where you can get the full list of modes in the extras section below.
  • The 'w' mode here means “write only” – It will attempt to create the file if it is not found. Please do also take note that it will truncate the existing file (remove all existing contents in the file), so please use this with caution.
  • fclose(STREAM) will properly close the file. Some online tutorials somehow miss this one out, but I will personally stress – Please close the files properly, or you migh get a corrupted file.

2. READING FILES

READ LINE-BY-LINE

2a-read.php
  • As with creating new files, we use the fopen function to read existing files – But we use 'r' mode instead of 'w'.
  • The 'r' mode means “read-only”, and it will place the file pointer at the start of the file (start reading from the top).
  • Next, we simply use the fgets(STREAM) function to read the file line-by-line.
  • Finally, remember to close the file, even if it is for reading only.

READ SPECIFIC BYTES

Opps. Have a file that does not have proper line breaks?

2b-fread.php
  • Instead of using fgets, we will use while (!feof) to continuously loop until the end of file.
  • Then using fread, we can specify the number of bytes to fetch from the file.

3. WRITE TO FILE

3-write.php
  • Same old story – Open the file with fopen, close with fclose when done.
  • To write data to the file, we use the fwrite(STREAM, DATA) function.

4. APPEND TO FILE (WRITE ARRAY TO FILE)

4-append.php
 "World", 
  "ID" => 123, 
  "Name" => "John Doe"
];
foreach ($data as $k=>$v) {
  $line = "[$k] - $v\r\n";
  fwrite($stream, $line);
}

// (3) CLOSE - MAKE SURE IT WRITES PROPERLY
fclose($stream) or die("Error closing " . $file);

// (4) FILE WRITE OK - DO YOUR THING
echo "OK";
?>
  • Appending to an existing file is as easy as changing the 'w' mode in fopen to 'a'.
  • However, it is a good idea to check if the file exists first.
  • Writing an array (multiple lines) to the file is also straightforward, just run through the array with a foreach loop and use \r\n to add line breaks.

5. COPY A FILE

5-copy.php

This should be very self-explanatory… Just use the copy(SOURCE, DESTINATION) function.

6. RENAME A FILE

6-rename.php

Yep, another self-explanatory process. Just use the rename(SOURCE, DESTINATION) function.

7. MOVE A FILE

7-move.php

Move uses the same rename function, except that we define a different destination folder.

8. DELETING FILES

8-delete.php

To delete a file, we use the unlink(FILE) function. Take note – it is permanent and will skip the recycle bin.

9. TOUCH!?

9-touch.php

The final file related PHP function that we are going to touch on, is touch(FILE, TIMESTAMP). Yep, this is kind of a weird function, as it only updates the modified date of the file and not change the contents… Might still be useful if you use the timestamp for some process?

EXTRA
USEFUL BITS AND LINKS

That’s all for this project, and here is a small section on some extras and links that may be useful to you.

THE SUMMARY

FunctionDescription
fopen(FILE, MODE)

Opens a selected file.

fclose(STREAM)Properly closes a file.
fgets(STREAM)Reads a single line from the file stream.
feof(STREAM)Is the file pointer at the end of the file?
fread(STREAM, BYTES)Reads a specific number of bytes from the file stream.
fwrite(STREAM, DATA)Writes data to file stream.
copy(SOURCE, DESTINATION)Copy a file.
rename(SOURCE, Target)Rename or move a file.
unlink(FILE)Delete a file.
touch(FILE, TIMESTAMP)Updates the “last modified” timestamp of the file.

REFERENCES

  • fopen
  • fclose 
  • fgets
  • feof
  • fread
  • fwrite
  • copy
  • rename
  • unlink
  • touch

LINKS

  • Need more ways to read files?
  • If you are having problems with the file paths.

INFOGRAPHIC CHEAT SHEET

PHP File Handling Basics (Click to Enlarge)

CLOSING
WHAT’S NEXT?

Thank you for reading, and we have come to the end of this guide. I hope that this PHP file yoga session has helped you to better understand. If you have anything to share with this guide, please feel free to comment below. Good luck and happy coding!

The post Basic PHP File Handling – Create Read Write Append Copy Move Delete appeared first on Code Boxx.



This post first appeared on Xxxxxxxxx, please read the originial post: here

Share the post

Basic PHP File Handling – Create Read Write Append Copy Move Delete

×

Subscribe to Xxxxxxxxx

Get updates delivered right to your inbox!

Thank you for your subscription

×