Delete file from a directory with php NO DATABASE
<p>I am stuck on my codes from 2 days now. I have already tried more then 100 tutorials/guide but none of them solve my problem. Mostly guide is for database.</p>

<p>I am using ready made gallery plugin to display images on my website. This gallery does not use database. For that I made admin panel with upload function. Now i am looking for function to delete uploaded photos from admin panel. one pic at a time or multiple option will be more then good.</p>

<p>Right now with this code i am displaying images in Admin Page at main.php which i uploaded before:</p>

<pre><code>&lt;?php
$folder_path = 'gallery-images/'; //image's folder path

$num_files = glob($folder_path . "*.{jpeg,jpg,gif,png,bmp}", GLOB_BRACE);

$folder = opendir($folder_path);

if($num_files &gt; 0)
{
 while(false !== ($file = readdir($folder))) 
 {
  $file_path = $folder_path.$file;
  $extension = strtolower(pathinfo($file ,PATHINFO_EXTENSION));
  if($extension=='jpg' || $extension =='png' || $extension == 'gif' || $extension == 'bmp') 
  {
   ?&gt;
            &lt;a href="&lt;?php echo $file_path; ?&gt;" id="fname"&gt;&lt;img src="&lt;?php echo $file_path; ?&gt;"  height="250" /&gt;&lt;/a&gt;
            &lt;?php
  }
 }
}
else
{
 echo "the folder was empty !";
}
closedir($folder);
?&gt;
</code></pre>

<p>And i am trying this delete code in delete.php</p>

<pre><code>&lt;?php
$filename = $_POST['fname'];
$path = $_POST['directory'];
if(file_exists($path."/".$filename)) { 
 unlink($path."/".$filename); //delete file
}
?&gt;
</code></pre>

<p>So i need a function to delete file from server with confirmation and with delete button. Right now file just open with a click. This function will be only for admin, So i think i am safe with delete function as i read in similar topics.</p>

<p>Thanks in Advance.</p>

<p><strong>EDIT 1:</strong></p>

<p>So far this code successfully delete a file from server (Answer from @Jocelyn):</p>

<pre><code>&lt;h3&gt;&lt;a href="?delete=1"&gt;Delete Now!&lt;/a&gt;&lt;/h3&gt;

&lt;?php
    if(isset($_GET['delete']))
    {
        unlink(__FILE__);
    }
?&gt;

Change this unlink(__FILE__); to unlink("$file_path");
</code></pre>

<p><strong>EDIT 2:</strong></p>

<p>Sorry, it does delete file from server but its deleting all the files in that directory.</p>

<p>Is there anyway to delete only one file which i click.</p>

<p>Right now all photos appearing from one link of code, i think thats the problem.</p>

<p>The link is this from which photos are appearing: </p>

<pre><code>&lt;a href="?delete=1" id="delete" name="delete"&gt;&lt;img src="&lt;?php echo $file_path; ?&gt;"  height="250" /&gt;&lt;/a&gt;
</code></pre>