PHP File Accessing

PHP

Writing to File

To inject more data into a file (like .txt for instance), use the 'file_put_contents()' function. Also make sure you have permission to change the file - its important, because if you can only read it depending on the permission, then you can't update it. Else you can:

Make sure to include 'FILE_APPEND' when you want to just update the file.

Reading File

When you want to know what's inside a .txt file with code, you can use this to let PHP read a file in one string:

Using "echo" we can make the content physically visible.

Checking if File Exists

To check existence of a specified file, you can do it like this:

'file_exists()' is the function that checks the file.

Deleting File

To remove a document, PHP has this feature called 'unlink':

This is the easiest and shortest code in PHP to manipulate a file.

Rename/Move Files

With the 'rename()' function, you can do 3 actions - rename document, move file's folder locations and also change folder names:

So this service can even change the names of folders and move them, not just files.

Creating Folder

'mkdir' can make new physical folders:

Its common when building a system, where pictures need to be stored somewhere. If the folder doesn't already exist, it creates one and starts collecting images.

Deleting Folder

To delete a folder, 'rmdir' is used for this. Also make sure you have the folder completely empty, else PHP don't want to remove it. Here is the instance:

The 'rm' means 'remove' and 'dir' is 'directory', just in case you didn't know why these function names are weird.

Reading Folder Items

The 'scandir' feature can read a directory's files and its subfolders:

But it can't read its subfolders files, only child files, which are only in the folder but not any other directory.

Copying File

'copy' func. can duplicate files (but not folders sadly). The reason it has 2 arguments ("source.txt", "destination.txt") is because you need to give the duplicate file a new name to avoid conflicts:

You can make a copy of a file to a folder too if you want, like existing directory - you can give any path to the new copy.

Read File Attributes

this is what the file reader can do: you can see created/last accessed time, view owner's ID of the document, watch permissions in a numerical form, check if its a file with right extension and is it a directory. Its simply meant for knowing what the document's insides are:

Here we included all features to give you a robust file reader system. it also checks if its a folder (just in case an user thought a name they remembered belonged to a file, but was actually a folder).

Low-Level-Access Func. 'fopen()'

In here, we are using the 'file_get_contents()' system to print a document's source code - this is built on top of the 'fopen()' feature and is used particularly for reading files:

When building with 'fopen()', the 'r' character means it can only read a file, while 'a+' mode means it can read and append stuff at the end of a document. just 'a' means it will add on top of the file.