php - How to delete a file from the server - Stack Overflow

I have a javascript function below where it removes an appended file name from .listImage when the user

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

But what I want to know is that when the user clicks on the delete button, I also want the file to be deleted from the server. How can this be done?

The folder the files are stored in is known as ImageFiles and the file name code on server side is $_FILES["fileImage"]["name"].

The uploading of files into the server is on a seperate page on a php script which is below:

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


      }


?>

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

But what I want to know is that when the user clicks on the delete button, I also want the file to be deleted from the server. How can this be done?

The folder the files are stored in is known as ImageFiles and the file name code on server side is $_FILES["fileImage"]["name"].

The uploading of files into the server is on a seperate page on a php script which is below:

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


      }


?>
Share Improve this question edited May 1, 2012 at 13:23 user1364441 asked May 1, 2012 at 13:16 user1364441user1364441 851 silver badge8 bronze badges 2
  • 1 Your code is prone to attacks. You should never store any server data like paths on the clients! You need to make a handler from where you ajax a unique id assigned only to that file. The handler then verifies the ID and will delete it on the server with out any client knowing any sort of paths or file names. Otherwise I can start deleting your whole system! – Piotr Kula Commented May 1, 2012 at 13:19
  • @ppumkin my actual php script where it uploads files is on a seperate age from the javascript, is there a way deleting a file from the server by storing it in that page? – user1364441 Commented May 1, 2012 at 13:30
Add a ment  | 

2 Answers 2

Reset to default 4

Check out this - http://www.php/unlink

Try:

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

(The unlink ;))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744790646a4593895.html

相关推荐

  • php - How to delete a file from the server - Stack Overflow

    I have a javascript function below where it removes an appended file name from .listImage when the user

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信