微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐