Blogger Information
Blog 43
fans 2
comment 2
visits 113123
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
thinkphp6 上传文件
朝游东海
Original
7766 people have browsed it
/**
 * 上传文件
 * @author 朝游东海
 * @param string $filename input框的name
 * @param string $filepath  存储路径
 * @param string $rule 验证规则
 * @param int $maxsize 允许文件上传的大小 默认为2m
 * @param bool $fileas 是否用原文件名上传保存 默认false
 * @return array
*/
public function upload($filename='file',$filepath='images',$rule='fileExt:jpg,jpeg,png,gif,pem|fileMime:image/jpeg,image/gif,image/png,text/plain',$maxsize=2097152,$fileas=false)
{
    $file = request()->file($filename);
    try {
        if($file->getSize() > $maxsize){
            return rMsg(0,'上传失败,文件超出大小,请选择'.floor($maxsize/1024/1024) . 'm以内的文件');
        }
        validate([$filename => $rule])->check([$filename => $file]);
        //$savename = Filesystem::disk('public')->putFile( $filepath, $file);  // /public/storage/images/5d16da691b5eb.png  根据内部方法生成文件名
        //$savename = Filesystem::disk('public')->putFileAs( $filepath.'/'.date('Ymd'), $file,$file->getOriginalName()); // /public/storage/images/Snipaste_2019-11-20_11-57-35.jpg  使用原文件名
        $savename = $fileas == false ? Filesystem::disk('public')->putFile( $filepath, $file) : Filesystem::disk('public')->putFileAs( $filepath.'/'.date('Ymd'), $file,$file->getOriginalName());
        return rMsg(1,'上传成功',['path'=>Filesystem::getDiskConfig('public', 'url') . '/' . str_replace('\\', '/', $savename)]);
    } catch (ValidateException $e) {
        return rMsg(0,$e->getMessage());
    }
}

采用layui的上传图片组件

后台处理

image.png

image.png

image.png


结果

image.png

image.png

image.png

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post