从哪里放置取消链接代码以从服务器中删除文件

时间:2012-05-01 15:14:31

标签: php javascript

我在下面有一个javascript函数,当用户点击“删除”按钮时,它会从.listImage中删除附加的文件名:

function stopImageUpload(success, imagefilename){

      var result = '';

      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';      
         $('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage">Delete</button><br/><hr/></div>'); 
      }
      else {
         result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
      }


      $(".deletefileimage").on("click", function(event) {

        $(this).parent().remove();

    });

      return true;   
}

如您所见,$(".deletefileimage").on("click", function(event) {用于删除正确的附加文件名。

但我想要做的是当用户删除文件名时,它还会从服务器中删除该文件。所以我试图使用这段代码:unlink($_FILES["fileImage"]["temp_name"]);从服务器上删除文件。

但我想知道的是,我在哪里存储此代码,以便它使用javascript函数删除附加的文件名,但是然后能够进入php脚本从服务器中删除文件?

以下是发送文件上传的php脚本(imageupload.php):

<?php

session_start();

$result = 0;

if( file_exists("ImageFiles/".$_FILES['fileImage']['name'])) {
    $parts = explode(".",$_FILES['fileImage']['name']);
    $ext = array_pop($parts);
    $base = implode(".",$parts);
    $n = 2;

    while( file_exists("ImageFiles2/".$base."_".$n.".".$ext)) $n++;
    $_FILES['fileImage']['name'] = $base."_".$n.".".$ext;

    move_uploaded_file($_FILES["fileImage"]["tmp_name"],
    "ImageFiles2/" . $_FILES["fileImage"]["name"]);
    $result = 1;

}
    else
      {
      move_uploaded_file($_FILES["fileImage"]["tmp_name"],
      "ImageFiles2/" . $_FILES["fileImage"]["name"]);
      $result = 1;

      }

?>

  <script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result ? 'true' : 'false'; ?>, '<?php echo $_FILES['fileImage']['name'] ?>');</script>

2 个答案:

答案 0 :(得分:3)

您需要对php脚本使用ajax调用,以允许删除服务器上的文件。可以找到JQuery的ajax文档here

首先,您需要连接按钮和图像文件名,如此

$('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage" image_file_name="' + imagefilename + '">Delete</button><br/><hr/></div>');

现在,您的删除方法将如下所示。

$(".deletefileimage").on("click", function(event) {
    // Find the image file name that is associated with this delete button. You 
    // may want to think about linking the image file name and the delete button
    // in a better way than this.
    var image_file_name = $(this).attr('image_file_name');
    $(this).parent().remove();
    console.log("Deleting " + image_file_name);
    jQuery.ajax("delete.php?imagefilename=" + image_file_name)
      .done(function(data) {
        $(".msg").append(data);
       });
});

最后,delete.php需要看起来像这样

<?php
  $image_file_name = "ImageFiles/" . $_GET["imagefilename"];
  if (User Uploaded this File || Has Permission to Delete it)
  {
    print "Deleting $image_file_name";
    // This assumes delete.php is in the same directory as the image file.
    unlink($image_file_name);
  }
?>

Here is a JSFiddle向您展示工作代码。

答案 1 :(得分:1)

最好将上传的文件存储在公共临时目录中,然后仅在保存内容时移动。

另一种方法是列出所有上传文件的标记为“活动/非活动”,并定期运行删除旧文件的脚本(非活动和创建的小时或几天前)。在这种情况下,当您添加文件或删除内容时,您将其标记为“非活动”,并在保存内容时将已使用的文件标记为“活动”。