删除图像上传器上的图像

时间:2014-10-18 18:29:39

标签: javascript php


我需要知道如何删除上传的图像或删除输入隐藏的值。 例如,进入if(document.getElementByID(“eliminar”)。click())。 非常感谢。

<script type="text/javascript" >
$(function(){
    var btnUpload=$('#upload');
    var status=$('#status');
    new AjaxUpload(btnUpload, {
        action: 'upload-file.php',
        name: 'uploadfile',
        nSubmit: function(file, ext){
            if(!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                // extension is not allowed 
                status.text('Only JPG, PNG or GIF files are allowed');
                return false;
            }
            //status.text('Uploading...');
            status.show();
        },
        onComplete: function(file, response){
            //On completion clear the status
            status.text('');
            //Add uploaded file to list
            if(response==="success"){
                $('<li></li>').appendTo('#files').html('<img src="./uploads/'+file+'"/><input type="hidden" name="uploadfile[]" value="'+file+'"><button name="eliminar" id="eliminar">Eliminar</button>').addClass('success');
                if(document.getElementById("eliminar").click()){

                }
            } else{
                $('<li></li>').appendTo('#files').text(file).addClass('error');
            }
        }
    });
});
</script>

那是我的upload-file.php

<?php
$uploaddir = './uploads/'; 
$file = $uploaddir . basename($_FILES['uploadfile']['name']);

if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
  echo "success"; 
} else {
    echo "error";
}
?>

1 个答案:

答案 0 :(得分:1)

unlink用于删除php中服务器上的文件

unlink("./uploads/filename");

发送了ajax请求并取消链接文件

function removeFile(){

$.ajax({ 
   url:'fileDel.php',
   data:'fllename',
   dataType:'json',
   success:function(){
       alert("file deleted");
   }
});
}