thinkphp6实现本地文件上传并移动文件位置
2022-11-24 11:10:29
171
{{single.collect_count}}

查看tp6文档,大家会发现tp6的文件上传存储路径是在runtime文件夹中的,有些同学需要将文件存储在自定义的其他路径中,该如何实现呢?

//上传图片文件

public function upimage(){

// 获取表单上传文件 例如上传了001.jpg

$file = request()->file('image');

// 上传到本地服务器

$savename = \think\facade\Filesystem::putFile('images' , $file);

$todayDate = date("Ymd",time());

//判断文件夹是否存在

if(!file_exists(__DIR__.'/../../../public/upload/images/'.$todayDate)){

mkdir(__DIR__.'/../../../public/upload/images/'.$todayDate);

}

$fileName = str_replace('images/'.$todayDate, '', $savename);

$movePath = __DIR__.'/../../../public/upload/images/'.$todayDate.$fileName;

$savePath = __DIR__.'/../../../runtime/storage/'.$savename;

if(!file_exists($savePath)){

return josn(['code'=>'no files']);

}

rename($savePath,$movePath);

return json(array(

'file' => $savename,

));

}

上面的实例代码中的文件路径需要根据您自己的实际情况去调整。

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}