gpt4 book ai didi

php - 如何从服务器删除文件

转载 作者:行者123 更新时间:2023-12-04 16:26:57 25 4
gpt4 key购买 nike

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


}


?>

最佳答案

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

关于php - 如何从服务器删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10398191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com