【问题标题】:Confirm before replacing an existing file in the folder using PHP在使用 PHP 替换文件夹中的现有文件之前确认
【发布时间】:2017-08-05 23:47:29
【问题描述】:

我有一个上传表单,我想显示一条确认消息

"目的地已经包含相同的文件。你想 代替它? - (显示两个按钮,替换和取消)"

它应该在将文件上传到目标文件夹之前弹出。

逻辑如下

if (CSV exists){
        if(replace file== yes) {
            upload file
        }else{
            do nothing.
        }
}else{
    upload file
}

我怎样才能对 PHP 做同样的事情? 注意:我正在使用 sweet alert 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>

我的代码不完整,因为在我选择替换选项或取消选项之前,文件已经被替换。知道如何解决这个问题。?

【问题讨论】:

    标签: php jquery mysql upload sweetalert


    【解决方案1】:

    您应该能够对 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;
    

    【讨论】:

      猜你喜欢
      • 2016-12-01
      • 2013-04-12
      • 2018-10-11
      • 2021-01-25
      • 1970-01-01
      • 2014-08-23
      • 2015-11-13
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多