FTP同名文件覆盖问题

浏览:5751 发布日期:2016/06/03
3.2.3 - 严重 - 未处理
设定rootpath/**
     * 检测上传根目录
     * @param string $rootpath   根目录
     * @return boolean true-检测通过,false-检测失败
     */
    public function checkRootPath($rootpath){
        /* 设置根目录 */
        $this->rootPath = ftp_pwd($this->link);

        if(!@ftp_chdir($this->link, $this->rootPath)){
            $this->error = '上传根目录不存在!';
            return false;
        }
        return true;
    }
重写save方法 /**
     * 保存指定文件
     * @param  array   $file    保存的文件信息
     * @param  boolean $replace 同名文件是否覆盖
     * @return boolean          保存状态,true-成功,false-失败
     */
    public function save($file, $replace=true) {
        $filename = $this->rootPath . $file['savepath'] . $file['savename'];
        /* 不覆盖同名文件 */
        if (!$replace) {
            ftp_chdir($this->link, $this->rootPath . $file['savepath']);
            $ftpFileList = ftp_nlist($this->link, ".");
            if(in_array($file['savename'], $ftpFileList)) {
                $this->error = '存在同名文件' . $file['savename'];
                return false;
            }
        }
        /* 移动文件 */
        if (!ftp_put($this->link, $filename, $file['tmp_name'], FTP_BINARY)) {
            $this->error = '文件上传保存错误!';
            return false;
        }
        return true;
    }
评论(
后面还有条评论,点击查看>>