得到文件的文件名 (不带扩展名)



 //得到文件的文件名,不带扩展名
    public function getFileName($arrFileName)
    {
        $newFlieNames = array();
        foreach($arrFileName as $key=>$value){
            $houzhui = substr(strrchr($value, '.'), 1);
            $newFlieNames[] = str_replace(".".$houzhui,'',$value);
        }
        //dd($newFlieNames);
        return $newFlieNames;
    }

应用:

         判断一个文件是否在该目录列表中


 //公司文件列表  目录遍历方法
        $directories = "D:\wamp\dmp_line2\uploads\cmp_001";
        $fileList = $this->listdir($directories);
        $file = new File();
        $fileList = $file->getFileName($fileList);

        $postFileName = iconv("UTF-8","GB2312",$filename);
        $postFileName = $file->getFileName(array($postFileName));
        $postFileName = implode('',$postFileName);

        $is_exits = in_array($postFileName,$fileList);

        dd($is_exits);

 //遍历一层目录  (统计有多少个文件
    public function listdir($dirname) {

        $ds = opendir($dirname);

        $filsList = array();
        while($file = readdir($ds)) {
            $path = $dirname.'/'.$file;
            if(is_dir($file)) {
                if($file != "." && $file != "..") {
                    listdir($file);
                }
            }
            else {
                $filsList[] = $file;
            }
        }
        return $filsList;
    }

前台

<pre name="code" class="html">    <pre name="code" class="html"><script type="text/javascript">
        function check(){
            var textF = document.getElementById("textfield");
            var filename = textF.value;  //取得上传文件名
            var reg = /\.(?:csv|xls|xlsx)$/i;

            //ajax请求判断文件夹内是存在该名称的文件,存在则给提示。注意此处要等后台的结果,所以用ajax同步
            var comId = "{{$comp_id}}";
            var is_exit = 0;
            $.ajax({
                type: 'post',
                url: "{{url('query/is_FileExist')}}",
                data: 'filename=' + filename+"&comId="+comId,
                async: false,  //ajax同步
                success: function (msg) {
                    //alert(msg);
                    if(msg == 1){
                        alert("您上传的文件存在同名,请重新选择上传!");
                        is_exit = 1;
                    }
                }
            });

            if(is_exit == 1){
                return false;
            }
            //var filename=cur;
            var index1=filename.lastIndexOf(".");
            //alert(index1);
            var index2=filename.length;
            //alert(index2);
            var postf=filename.substring(index1,index2);//后缀名
            alert(postf);

            if(reg.exec(postf)){
                alert(" 上传成功")
            }else{
                alert(" 上传失败")
                return false;
            }

            /*var textF = document.getElementById("textfield");
            var cur = textF.value;
            var reg = /(\.xls|\.xlsx|\.csv)/ig;
            if(reg.test(cur)){
                alert(" 上传成功")
            }
            else{
                alert(" 上传失败")
                return false;
            }*/
        }
    </script>
</head>

 
 
 
 

FORM表单

<form method="POST" action="/public/query" enctype="multipart/form-data" id="act" onsubmit="return check()">


你可能感兴趣的:(得到文件的文件名 (不带扩展名))