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
A1WEBSITEPRO Social Media Pages
Here are my social media pages, lets hook up!