thinkphp将上传的临时文件移动到指定目录

thinkphp将上传的临时文件移动到指定目录

新建common.php文件

<?php
use think\facade\Env;

/** 移动上传的临时文件
*
* @img_dir string 存储路径
* @file array 上传的临时文件路径
*/
function move_temp_images($img_dir, $file)
{
$root_path = Env::get('root_path').'public/';
// 目录是否存在 不存在则创建
if (!file_exists($root_path . $img_dir)) {
create_dir($root_path . $img_dir);
}
if(is_array($file)) {
$images = [];
foreach ($file as &$image) {
if(stripos($image, 'temp/uploads') !== false && file_exists($root_path . $image)) {
$img_name = basename($image);
if ($img_name && @rename($root_path . $image, $root_path . $img_dir . $img_name)) {
$images[] = $img_dir . $img_name;
}
}
}
$images = serialize($images);
}else{
if(stripos($file, 'temp/uploads') !== false && file_exists($root_path . $file)) {
$img_name = basename($file);
if ($img_name && @rename($root_path . $file, $root_path . $img_dir . $img_name)) {
$images = $img_dir . $img_name;
}
}
}
return $images;
}


2.调用
新建test.php文件

<?php

...
...
$test_img ='temp/uploads/a.png' //上传图片临时文件路径
$img_dir = 'images/test/' .date('Ymd') . '/'; //指定的存储路径
$new_img = move_temp_images($img_dir, $test_img);

转载于:https://www.cnblogs.com/yuuje/p/10992627.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值