2011-03-30 35 views
1

我有一個使用PHP從文件夾中刪除圖像的腳本。如何使用PHP顯示要刪除的圖像取消鏈接

問題是圖像沒有顯示在頁面上。

我的代碼如下

<?php 
// directory separator 
defined("DS") 
    || define("DS", DIRECTORY_SEPARATOR); 

// root path 
defined("ROOT_PATH") 
    || define("ROOT_PATH", realpath(dirname(__FILE__))); 

// upload folder directory 
defined("UPLOAD_DIR") 
    || define("UPLOAD_DIR", "uploads"); 

// path to the upload folder 
defined("UPLOAD_PATH") 
    || define("UPLOAD_PATH", ROOT_PATH.DS.UPLOAD_DIR); 


function getAllFiles($folder = null) { 
    if(!empty($folder) && is_dir($folder)) { 
     if($handle = opendir($folder)) { 
      $out = array(); 
      while($file = readdir($handle)) { 
       if(is_file($folder.DS.$file)) { 
        $out[] = $file; 
       } 
      } 
      closedir($handle); 
      return $out; 
     } 
     return false; 
    } 
    return false; 
} 

$files = getAllFiles(UPLOAD_PATH); 

if (!empty($_POST['file'])) { 
    foreach($_POST['file'] as $file) { 
     unlink(UPLOAD_PATH.DS.$file) or die("Failed to <strong class='highlight'>delete</strong> file"); 
    } 
    header("location: " . $_SERVER['REQUEST_URI']); 
} 
?> 

<?php if (!empty($files)) { ?> 

<form name="form1" method="post"> 
    <?php foreach($files as $key => $file) { ?> 
     <label for="file_<?php echo $key; ?>"> 
      <input type="checkbox" name="file[]" id="file_<?php echo $key; ?>" value="<?php echo $file; ?>" /> 
      <?php echo $file; ?> 
     </label> 
    <?php } ?> 
    <input type="submit" name="delete" value="Delete" /> 
</form> 

的代碼工作正常刪除的圖像,但不顯示頁面上的圖像。

您的幫助非常感謝。

謝謝

+1

我沒有看到任何代碼在您的腳本中顯示任何圖像。你能澄清這是應該做的嗎? – 2011-03-30 09:44:02

+0

+1爲您的條件定義'defined(「DS」)|| define(「DS」,DIRECTORY_SEPARATOR);'作爲一個很好的方式來做到這一點,從來沒有想過這樣做 – RobertPitt 2011-03-30 09:44:43

+0

@Pekka +1,我想知道這不僅僅是一個缺少'「/>'的形式... – ybo 2011-03-30 09:46:02

回答

0

我認爲圖像仍然在瀏覽器緩存,所以刪除緩存將導致沒有看到任何刪除的圖像。

相關問題