2

我在下面有一个 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;   
}

但我想知道的是,当用户点击删除按钮时,我也希望将文件从服务器中删除。如何才能做到这一点?

存储文件的文件夹称为 ImageFiles,服务器端的文件名代码为$_FILES["fileImage"]["name"].

将文件上传到服务器是在下面的 php 脚本的单独页面上:

<?php
$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("ImageFiles/".$base."_".$n.".".$ext)) $n++;
    $_FILES['fileImage']['name'] = $base."_".$n.".".$ext;

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

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


      }


?>
4

2 回答 2

5

看看这个 - http://www.php.net/unlink

于 2012-05-01T13:18:34.180 回答
3

尝试:

<?php
// Delete image from server
unlink($path_to_file);
?>

(取消链接;))

于 2012-05-01T13:20:56.690 回答