• 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);
  • 相关阅读:
    win10 创建安卓模拟器及启动的方法
    win10 virtualenv
    win10安装nodejs
    python模块打包方法
    win10 安装java
    git push后自动部署
    ubuntu配置无密码登录
    mysql while,loop,repeat循环,符合条件跳出循环
    centos 安装mysql密码修改后还是不能连接的原因
    查看SQLServer数据库信息的SQL语句
  • 原文地址:https://www.cnblogs.com/yuuje/p/10992627.html
Copyright © 2020-2023  润新知