Hey everybody I want to let you know that I have undertaken the grueling task of getting the heck away from WordPress. I was so sick of the problems and updates I had to do all the time. I am now using my ezbloo system and I am integrating all my old posts into the new system. It sucks, but in the end, I will save bundles of time. I needed to keep things simple and that is why I created ezbloo. I'll have more on this later for you guys after I am done with the total integration of my old posts here. So if you are looking for a post and need it faster, shoot me an email and I will make it a priority. [email protected]

In order to delete an image or file off of your server in PHP you have to use unlink(). This function will delete the file from your server. This is a permanent action and can not be undone once we completed the process. If you're getting warning: unlink(): http does not allow unlinking in PHP, then I will show you how to resolve that error.

Warning: unlink(): http does not allow unlinking in ...

If you are getting the warning, "Warning: unlink(): HTTP does not allow unlinking in path-to-filename". You are using an HTTP request to delete this file. This is wrong to use. You have to use the absolute path to delete the file from your server.

How To Use unlink() Properly in PHP

In order to use unlink properly, you will have to know the absolute path to the directory. The directory is the path to the folder where your images are located. Many programmers call this folder "uploads" because it makes sense.  Here is what an absolute path looks like using the unlink function in PHP.
unlink('/var/www/uploads/2015/image_name.jpeg');

How to get an Absolute Path on the Website in PHP

So your next question will be how to get that absolute path in PHP? PHP has a function for that as well. Here is the code to get an absolute path in PHP.
<?php echo $_SERVER['DOCUMENT_ROOT']; ?>
This PHP code will echo out something like/Applications/XAMPP/xamppfiles/htdocs if you are working locally on a server. However, if you are working on a live server it will echo out something like
/home/user/public_html/
You can store this path in a PHP variable and use it in your programming.
<?php 
$abspath=$_SERVER['DOCUMENT_ROOT']; 
echo $abspath;
?>

Getting A More Accurate Absolute Path

The above examples only give you the absolute path to the root of your website. However, let's say that you are in a directory like the admin directory, and you want the absolute path to it. There is another PHP function that will handle this for you called getcwd().
$pathtodir = getcwd();
    echo "Absolute Path To Directory is: ";
    echo $pathtodir;
This will give you something like this "/Applications/XAMPP/xamppfiles/htdocs/ezbloo/admin" on a local server. If you are working on a live or remote server then the path will look something like this.
/home/user/public_html/admin/
Remember, if you use getcwd() it is only going to work in the directory that you put it in. In the above example, we put it in the "admin" directory, and it gave us an absolute path to it. If we were working in another directory like "admin/folder" it would give us a path like this.
/home/user/public_html/admin/folder
https://youtu.be/ecZ2qeIOS6E Files for examples.

Another Way To Get An Accurate Path in PHP

You can also use the function dirname(__FILE__); This will do the exact same thing as getcwd(). You can store it in a variable as well to get the path. Most of the time when programmers want to get a path to a directory they are working in they will use this function.
 $abpath =dirname(__FILE__);
 echo $abpath;

Example of using Unlink() in PHP

So now, let's say that I have image uploads in a folder called "upload" outside the admin directory. I want to give users the ability to delete their images and files using unlink. They are working in an admin directory to delete a file in a different directory called uploads. Here is an example of the code.
if(isset($_POST['submit'])){
$file=$_POST['file'];
$abspath=$_SERVER['DOCUMENT_ROOT'];
$dir = $abspath.'/includes/upload/';
unlink($dir."".$file);
 }
I could not use the other two methods because I am deleting files outside the admin directory. So I used $_SERVER['DOCUMENT_ROOT'] and concatenated manually the path to the folders "/includes/upload/". If you were deleting images in the admin directory, you could use the other two methods. :-) If you need more help, I created a post here with codes that show you how to delete files in a directory with PHP unlink.