【问题标题】:Delete Image Via Radio Button通过单选按钮删除图像
【发布时间】:2012-04-25 23:26:33
【问题描述】:

我想知道是否有人可以帮助我。

我已经将创建图片库的脚本放在了下面。

<?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="&lt;?php echo $name; ?&gt;" src="&lt;?php echo $thumbnail; ?&gt;" /&gt;&lt;/a&gt;&lt;?php echo "&lt;input type='radio' name='del' value='{$originalname}' /&gt;"?&gt;&lt;/li&gt; 我添加了一个单选按钮。

在选择单选按钮并单击“删除选定图像”按钮后,我希望在此文件路径中删除图像:

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

这里:$thumbnailsPath = $galleryPath . 'Thumbnails/';

我对 PHP 并不精通,但我做了一些研究,我相信我需要使用“取消链接”命令。所以我想我会尝试一开始只使用一个文件路径来编写这个过程。我可以选择一个单选按钮,然后显示正确的Dialog Confirmation 消息,但不幸的是它不会删除图像,而且我不确定问题出在哪里。

我只是想知道是否有人可以看看这个并让我知道我哪里出错了?

非常感谢和问候

【问题讨论】:

    标签: php


    【解决方案1】:

    如果您知道将正确的图像路径传递给 unlink 命令,则运行 PHP 的用户帐户可能没有文件的写入权限。

    您可能希望在 E_WARNING 级别打开错误,然后运行测试脚本以查看是否收到警告。您还应该测试 unlink 的返回值,因为它在失败时返回 FALSE。

    【讨论】:

    • 您好,感谢您抽出宝贵时间回复。我遇到的问题是我没有收到任何错误消息,这可能表明原因是代码而不是权限问题。但是因为我对 PHP 有点陌生,所以我不确定我的代码是否有问题。我真的很想确认,尽管我很欣赏这可能只是假设性的,我的代码是否正确?亲切的问候
    • 这一行的语法是错误的,你缺少一个点来连接最后两个字符串: $file = 'UploadedFiles/' 。 $_SESSION['用户名'] 。 '/' 。 $_SESSION['locationid'] 。 '/' '$原始名称'; vs $file = 'UploadedFiles/' 。 $_SESSION['用户名'] 。 '/' 。 $_SESSION['locationid'] 。 '/' 。 '$原始名称';看出区别了吗?
    • 嗨@Greg,非常感谢。我明白你的意思了。我会做出改变。亲切的问候
    • 哎呀,我忘记了一部分。使用单引号时,不会插入变量。您应该从 $originalname 中拉出单引号,因为它将其视为字符串文字,而不是变量的值。
    猜你喜欢
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2014-04-17
    • 2012-06-18
    相关资源
    最近更新 更多