我有一个上传表单,我想显示一条确认消息
"目标已包含相同的文件。你想要_____吗 代替它? - (显示两个按钮,替换和取消)"
在将文件上传到目标文件夹之前应该弹出。
逻辑如下:
if (CSV exists){
if(replace file== yes) {
upload file
}else{
do nothing.
}
}else{
upload file
}
我怎么能用PHP做同样的事情? 注意:我正在使用甜蜜警报jquery来定制弹出消息。示例代码如下所示
<script>
$().ready(function () {
/*swal*/
swal({
title: "File already exists!",
text: "CSV file is already existing in the folder, do you want to replace it?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Replace",
closeOnConfirm: false,
closeOnCancel: true
},
function(isConfirm){
if (!isConfirm)return;
if (isConfirm == true) {
}
});//is confirm function
/*swal*/
});//document ready
</script>
我的代码不完整,因为即使在我选择替换选项或取消选项之前,文件已经被替换。任何想法如何解决这个问题。?
答案 0 :(得分:0)
你应该能够对PHP文件做一个AJAX请求,如果文件存在与否,它可以返回消息。在PHP中,您的逻辑将是:
if( file_exists($path_to_csv) ){
if( $replace === TRUE ) {
file_put_contents($path_to_csv, $data);
$ret = json_encode(['type'=> 'warning', 'text' =>'File existed and was replaced.']);
} else {
$ret = json_encode(['type'=> 'error', 'text' => 'File already exists and will not be replaced.']);
}
} else {
file_put_contents($path_to_csv, $data);
$ret = json_encode(['type'=> 'message', 'text' => 'File created']);
}
echo $ret;