Delete File

Deleting files in php isn’t extremely common, but it is sometimes necessary. Always remember that you should hesitate before deleting anything. PHP doesn’t have a special undo for what you delete. Now that I have warned you, let’s burn some files to the ground.

Unlink in PHP

Example
$myFile = "testFolder/sampleDeleteFile.txt";
unlink($myFile) or die("Couldn't delete file");

As you can see, it doesn’t take too much to delete a file. If you run into problems deleting files, try opening and closing the file before you actually delete it. Like so:

Example
$myFile = "testFile.txt";
$myFileLink = fopen($myFile, 'w') or die("can't open file");
fclose($myFileLink);
$myFile = "testFolder/sampleDeleteFile.txt";
unlink($myFile) or die("Couldn't delete file");

I should note that this is more of a hack rather than good practice. Try to always close your files shortly after you open them. All you are doing with the code above is opening the file again to ensure that you actually close it.

Overview

PHP works on the Server Side (on the server hosting the website).


How PHP works

A single PHP file is like using magic on a webpage. In HTML, you have a standard page that is sent to anyone that visits your website. With PHP, you can dynamically change the page based on each individual user. Honestly, learn PHP and put it to the test. You will be extremely impressed with what an open source programming language can accomplish. I am not going to tell you that PHP is the best server side language that is out there, but it is the best for a tight budget or a beginner (Sorry, after coding with ColdFusion, I can no longer say PHP is my favorite).

What exactly does PHP do?

It can create custom content based on different variables

It is excellent at tracking user information

It can write or read information to databases, if partnered with a database language

It can run on any type of platform and servers

It can do anything a standard HTML file can… and much more