2012-04-25 113 views
0

我想知道是否有人能夠幫助我。通過單選按鈕刪除圖像

我把下面的腳本放在一起,創建一個圖片庫。

<?php 

    if (isset($_POST['del'])) 
     $originalname = intval($_POST['del']); 

     $file = 'UploadedFiles/' . $_SESSION['username'] . '/' . $_SESSION['locationid'] . '/' '$originalname'; 
if (!unlink($file)) 
    { 
    echo ("Error deleting $file"); 
    } 
else 
    { 
    echo ("Deleted $file"); 
    } 
?> 

<?php 

    $galleryPath = 'UploadedFiles/' . $_SESSION['username'] . '/' . $_SESSION['locationid'] . '/'; 

    $thumbnailsPath = $galleryPath . 'Thumbnails/'; 

    $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR; 

    $descriptions = new DOMDocument('1.0'); 
    $descriptions->load($absGalleryPath . 'files.xml'); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Gallery</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <link href="Libraries/fancybox/jquery.fancybox-1.3.1.css" rel="stylesheet" type="text/css" /> 
    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/base/jquery-ui.css" type="text/css" media="all" /> 
    <link href="Styles/style.css" rel="stylesheet" type="text/css" /> 
    <!--[if IE]> 
    <link href="Styles/ie.css" rel="stylesheet" type="text/css" /> 
    <![endif]--> 
    <script src="Libraries/jquery/jquery-1.4.3.min.js" type="text/javascript"></script> 
    <script src="Libraries/fancybox/jquery.fancybox-1.3.1.pack.js" type="text/javascript"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js" type="text/javascript"></script>   
    <script type="text/javascript"> 

    $(function() { $('a.fancybox').fancybox(); }); 

    </script> 
    <style type="text/css"> 
<!-- 
.style1 { 
    font-size: 14px; 
    margin-right: 110px; 
} 
.style4 {font-size: 12px} 
--> 
    </style> 
    <script type="text/javascript"> 
    function showdialog() { 
     // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore! 
     $("#dialog:ui-dialog").dialog("destroy"); 

     $("#dialog-confirm").dialog({ 
      resizable: false, 
      height:160, 
      modal: false, 
      buttons: { 
       "Delete image": function() { 
        $(this).dialog("close"); 
       }, 
       Cancel: function() { 
        $(this).dialog("close"); 
       } 
      } 
     }); 
    }; 
    </script> 


</head> 
<body style="font-family: Calibri; color: #505050; font-size: 9px; border-bottom-width: thin; margin-top: 5px; margin-left: -476px; margin-right: 1px; margin-bottom: -10px;"> 
<div align="right" class="style1"> <a href = "javascript:document.gallery.submit()"/> Add Images <a/> &larr; View Uploaded Images </div> 
    <form id="gallery" name="gallery" class="page" action="index.php" method="post"> 
    <div id="container"> 
    <div id="center"> 
     <div class="aB"> 
     <div class="aB-B"> 
      <?php if ('Uploaded files' != $current['title']) :?> 
      <?php endif;?> 
      <input name="deleteimage" type="button" value="Delete Selected Image" onclick="showdialog()" /> 
      <div class="demo"> 
      <div class="inner"> 
       <div class="container"> 
       <div class="gallery"> 
        <ul class="gallery-image-list"> 
        <?php for ($i = 0; $i < $descriptions->documentElement->childNodes->length; $i++) : 
          $xmlFile = $descriptions->documentElement->childNodes->item($i); 
          $name = htmlentities($xmlFile->getAttribute('originalname'), ENT_COMPAT, 'UTF-8'); 
          $description = htmlentities($xmlFile->getAttribute('description'), ENT_COMPAT, 'UTF-8'); 
          $source = $galleryPath . rawurlencode($xmlFile->getAttribute('source')); 
          $thumbnail = $thumbnailsPath . rawurlencode($xmlFile->getAttribute('thumbnail')); 
        ?> 
        <li class="item"> 
         <a class="fancybox" target="_blank" rel="original" href="<?php echo $source; ?>"><img class="preview" 
         alt="<?php echo $name; ?>" src="<?php echo $thumbnail; ?>" /></a><?php echo "<input type='radio' name='del' value='{$originalname}' />"?></li> 
         <p><span class="style4"><b>Image Name:</b> <?php echo htmlentities($xmlFile->getAttribute('originalname'));?> <br /> 
          <b>Image Description:</b> <?php echo htmlentities($xmlFile->getAttribute('description'));?> </span><br /> 
          <?php endfor; ?> 
          </li> 
         </p> 
        </ul> 
       </div> 
       </div> 
      </div> 

    </div> 
    <div id="dialog-confirm" title="Delete This Image?"> 
    <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>This image will be permanently deleted and cannot be recovered. Are you sure?</p> 
</div> 
    </div> 
     <div class="aB-a">  </div> 
     </div> 
    </div> 
    </div> 
    </form> 
</body> 
</html> 

針對每個圖像,並且出現在這行我的代碼:alt="<?php echo $name; ?>" src="<?php echo $thumbnail; ?>" /></a><?php echo "<input type='radio' name='del' value='{$originalname}' />"?></li>我添加了一個單選按鈕。

當單選按鈕被選擇,並且被點擊「刪除選擇的圖像」按鈕我想圖像以在該文件路徑被刪除:

$galleryPath = 'UploadedFiles/' . $_SESSION['username'] . '/' . $_SESSION['locationid'] . '/'; 

和這裏:$thumbnailsPath = $galleryPath . 'Thumbnails/';

I」在PHP中並不是很出色,但我做了一些研究,我相信我需要使用'unlink'命令。所以我想我會試着用最初的一個文件路徑來編寫這個程序。我可以選擇一個單選按鈕,並獲得正確的Dialog Confirmation消息出現,但不幸的是它不會刪除圖像,我不確定問題出在哪裏。

我只是想知道是否有人可能不想看看這個請讓我知道我哪裏出了問題?

非常感謝和問候

回答

0

如果你知道你傳遞正確的路徑圖像取消鏈接命令,就可以運行PHP的用戶帳戶不具有對文件的寫權限。

您可能希望在E_WARNING級別打開錯誤,然後運行測試腳本以查看是否收到警告。您還應測試取消鏈接的返回值,因爲它在失敗時返回FALSE。

+0

嗨,感謝您花時間回覆。我遇到的問題是我沒有收到任何錯誤信息,這可能表明代碼,而不是權限問題是原因。但是因爲我對PHP有點新,所以我不確定我的代碼是否有問題。我真的很想確認,但我明白,這可能只是強大的,我的代碼是正確的?親切的問候 – IRHM 2012-04-25 15:32:04

+0

這條線的語法是錯誤的,你錯過了加入最後兩個字符串的點: $ file ='UploadedFiles /'。 $ _SESSION ['username']。 '/'。 $ _SESSION ['locationid']。 '/''$ originalname'; vs $ file ='UploadedFiles /'。 $ _SESSION ['username']。 '/'。 $ _SESSION ['locationid']。 '/'。 '$ ORIGINALNAME'; 看到區別? – gcochard 2012-04-25 15:34:12

+0

嗨@Greg,非常感謝。我明白你的意思。我會做出改變。親切的問候 – IRHM 2012-04-25 16:58:39