douluolan9101 2012-05-01 15:14
浏览 26
已采纳

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

I have a javascript function below where it removes an appended file name from .listImage when the user clicks on the "Delete" button:

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;   
}

As you can see the $(".deletefileimage").on("click", function(event) { is used to delete the correct appended file name.

But what I want to do is that when the user deletes a file name, it also deletes the file from the server. So I am trying to use this code: unlink($_FILES["fileImage"]["temp_name"]); to delete the file from the server.

But what I want to know is that where do I store this code so that it uses the javascript function to delete the appended file name but then be able to go onto the php script to delete the file from the server?

BELOW IS the php script (imageupload.php) where the uploading of files occur:

<?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条回答 默认 最新

  • dousonghs58612 2012-05-01 15:21
    关注

    You will need to use an ajax call to your php script to allow deletion of files on the server. JQuery's ajax documentation can be found here.

    First, you will need to connect your button and image file name like so

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

    Now, your delete method will then look something like this.

    $(".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);
           });
    });
    

    Finally, the delete.php will need to look something like this

    <?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 to show you working code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做