通过单击对应按钮删除图像

I have a folder / directory containing lots of images and other folders / directories.

I am showing the preview of these files using following code:

<?php
      $images=array();  
        $dir_handler = opendir('test') or die("Unable to open path");  
        $i=0;    
        while($file = readdir($dir_handler))
        {            
        if(is_dir($file)) 
        continue;        
        else if($file != '.' && $file != '..' && $file != 'index.php')
        {                    
        $images[$i]=$file;
        $i++;     
        }       
        }      
        sort($images);

        for($i=0; $i<sizeof($images); $i++) 
        {              

        echo "<a href=".chr(34).$path.$images[$i].chr(34)."><img style='border:1px solid #666666; width:100px;height:100px; margin: 10px;' src='test/".$images[$i]."'/><input type='button' value='nok[]'></a>";

        }        closedir($dir);
?>

The problem is that I want to assign a separate button to each file (to each image or folder), So that by clicking each button, its corresponding image (or folder / directory) gets deleted from the main folder and no more being shown as a preview.

Another small problem is that the preview of folders are not being shown with the above code. Why? Any help will be appreciated.

You are always displaying an image, you need to do a check if the $images array value is an image or a folder. Then do something different with the display.

To delete a file or folder, use these functions respectively:

unlink();
rmdir();

http://www.php.net/manual/en/function.rmdir.php

http://php.net/manual/en/function.unlink.php

To add a list of folders before your images change your code to:

<?php
  $images=array();  
  $folders = array();
    $dir_handler = opendir('test') or die("Unable to open path");  
    $i=0;    
    while($file = readdir($dir_handler))
    {            
    if(is_dir($file)) {
         $folders[count($folders)] = $file;
    }   
    else if($file != '.' && $file != '..' && $file != 'index.php')
    {                    
    $images[$i]=$file;
    $i++;     
    }       
    }      
    sort($images);

    foreach($folders as $folder){

     echo "<a href='#'>$folder</a>";
    }

    for($i=0; $i<sizeof($images); $i++) 
    {              

    echo "<a href=".chr(34).$path.$images[$i].chr(34)."><img style='border:1px solid #666666; width:100px;height:100px; margin: 10px;' src='test/".$images[$i]."'/><input type='button' value='nok[]'></a>";

    }   

 closedir($dir);
?>

Then all you have to do is build out the visual elements of a folder in your view and link to a function that will do your unlink() and rmdir() where needed.

There are several questions here.

First question: Why won't preview of folders show? This is because you show previews by looping through the $images array, but you do not add directories to that array (see your code where you check if it is_dir and then invoke "continue;"). If you want to include the directories, then you should include them in the $images array (or do something else with them).

Second question: How to do the deleting? You need to either extend your existing PHP script or write another one. You would create a link on the delete icon; the link's href would be to the new (or existing) PHP script and you would pass in as parameters the folder or file to be deleted. If it is a folder, then use rmdir(). If it is a file, then use unlink(). I can help you more with this later, if you need it.