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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM