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

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

新建common.php文件

<?php
use thinkfacadeEnv;

/** 移动上传的临时文件
*
* @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