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

时间:2013-02-05 16:45:34

标签: php javascript image file button

我有一个包含大量图像和其他文件夹/目录的文件夹/目录。

我正在使用以下代码展示这些文件的预览:

<?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);
?>

问题是我想为每个文件(每个图像或文件夹)分配一个单独的按钮,这样通过单击每个按钮,其相应的图像(或文件夹/目录)将从主文件夹中删除,不再有被显示为预览。

另一个小问题是上面的代码没有显示文件夹的预览。为什么?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您始终显示图像,您需要检查$ images数组值是图像还是文件夹。然后用显示器做一些不同的事情。

要删除文件或文件夹,请分别使用以下功能:

unlink();
rmdir();

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

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

在图片更改代码之前添加文件夹列表:

<?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);
?>

然后你要做的就是在视图中构建文件夹的可视元素,并链接到一个函数,该函数将在需要的地方执行unlink()和rmdir()。

答案 1 :(得分:0)

这里有几个问题。

第一个问题:为什么不预览文件夹?这是因为您通过循环遍历$ images数组来显示预览,但是您没有向该数组添加目录(请参阅代码,在其中检查是否为is_dir然后调用“continue;”)。如果你想包含目录,那么你应该将它们包含在$ images数组中(或者用它们做其他的事情)。

第二个问题:如何删除?您需要扩展现有的PHP脚本或编写另一个脚本。您将在删除图标上创建一个链接;链接的href将是新的(或现有的)PHP脚本,您可以将要删除的文件夹或文件作为参数传入。如果是文件夹,则使用rmdir()。如果是文件,则使用unlink()。如果你需要,我可以在以后帮助你。

相关问题