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