1 Star2 Stars3 Stars4 Stars5 Stars (14 votes, average: 5.00 out of 5)

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

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 the process is completed.

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 a 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.

Tip: Click on images to make them larger. If you find anything wrong on this page or need further assistance please comment below or contact me. 🙂

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 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.

Subscribe To My YouTube Channel:
<?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

Warning: unlink(): http does not allow unlinking in PHP was last modified: March 8th, 2019 by Maximus Mccullough

Please like, share and subscribe.

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider a tip for the content.

One Time Tip

more tips

Reoccuring Tips

Tips

Cool People Share:
Warning: unlink(): http does not allow unlinking in PHP

Pages: 1 2 Next

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.