2015-09-11 38 views
1

我用php製作了一個照片庫。
我可以製作上傳圖片並顯示圖片列表。但我想添加一個刪除按鈕,用戶可以在那裏刪除不需要的圖像。但我沒有添加這個按鈕的工作。如何在我的照片庫添加刪除php腳本

有人請幫忙解決嗎?

這是我在GitHub代碼:https://github.com/sagar290/photo_gallerey/blob/6cad3743e735ea109723de7e14d5477bff1e964b/gallery.php

<?php 

if (isset($_FILES["file"]["name"])) { 
    $name = $_FILES['file']['name']; 
    //$size = $_FILES['file']['size']; 
    //$extention = $type; 
    $type = strtolower($_FILES['file']['type']); 
    $tmp_name = $_FILES['file']['tmp_name']; 


    if (isset($name)) { 
    if (!empty($name)) { 
     if (($type=='image/jpg')||($type=='image/jpeg')||($type=='image/gif')) { 
     $location = 'photos/'; 

     if(move_uploaded_file($tmp_name, 'photos/'.$name)) { 
      echo $name.' is uploaded'; 

     } 
     }else { 
     echo 'file must be jpg or jpeg '; 
     } 


    }else { 
     echo 'please choose a file'; 
    } 
    } 
} 

?> 

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <title>Photo gallery</title> 

    <style type="text/css"> 
     ul { 
      list-style-type: none; 
     } 
     li { 
     float: left; 
     padding: 10px; 
     margin: 10px; 
     font: bold 10px Verdana, sans-serif; 
     } 
     img { 
     display: block; 
     border: 1px solid #333300; 
     margin-bottom: 5px; 
     } 
    </style> 

    </head> 
    <body> 
    <h2>Photo gallery</h2> 

     <form action="gallery.php" method="POST" enctype="multipart/form-data"> 
     UPLOAD: 
     <input type="file" name="file"><br><br> 
     <input type="submit" value="submit"> 
    </form> 

     <ul> 
     <form action="gallery.php" method="POST"> 
<?php 


    //define location of photo image 
    $photosDir = './photos'; 

    //define which file extention are images 
    $photosExt = array('gif','jpg','jpeg','tif','tiff','bmp','png'); 

    //initialize array to hold filenames of images found 
    $photoList = array(); 

    //read directory contents 
    //build photo list 
    if (file_exists($photosDir)) { 
     $dp = opendir($photosDir) or die('Error: cannot open file'); 
     while ($file = readdir($dp)) { 
     if ($file != '.' && $file != '..') { 
      $fileData = pathinfo($file); 
      if (in_array($fileData['extension'], $photosExt)) { 
      $photoList[] = "$photosDir/$file"; // file includes as an array 
      } 
     } 
     } 
     closedir($dp); 
    }else { 
     die('ERROR: directory dosent exists'); 
    } 

    //itarate over photo list 
    //display each image and file name 
    if (count($photoList)>0) { 
     for ($x=0; $x <count($photoList) ; $x++) { 
     ?> 

     <li> 
      <input type="submit" name="delete" value="Delete"> 
      <img src="<?php echo $photoList[$x]; ?>" height="150" width="200"/> 
      <?php echo basename($photoList[$x]); ?><br> 
      <?php echo round(filesize($photoList[$x])/1024) . 'KB'; ?> 
     </li> 

     <?php 
     } 
    } else { 
     die('ERROR: No image found'); 
    } 





?> 

    </form> 
    </ul> 
    </body> 
</html> 
+0

我可以」 t在那裏看到任何試圖刪除圖像的代碼 – Popnoodles

+0

'

  • +0

    我明白了。它不告訴PHP要刪除哪個圖像。有人提供了一個答案無論如何 – Popnoodles

    回答

    0

    你好只是改變你的提交輸入:

    <input type="submit" name="delete" value="<?php echo $photoList[$x] ?>"> 
    

    ,然後再檢查該值:

    <?php if(isset($_POST['delete']) && !empty($_POST['delete'])){ 
        //find the file 
        $file = 'photos/'.$_POST['delete']; 
        if(is_file($file)){ 
         unlink($file); 
        }else{ 
         echo $_POST['delete']." has not been found!"; 
        } 
    }?> 
    
    +0

    你錯過了所有的單引號 – Grork

    +0

    tks,複製粘貼會這樣做給你,輸入錯誤一次.... –

    +0

    它仍然無法正常工作,我不能刪除圖像:( –