php 文件上传类

1.封装文件上传类方法

class UploadFile
{

private $error;

public function batchUpload($files, $name, $savePath, $type = [])
{
    $files = $this->convert($files);
    if (empty($files)) {
        $this->error = 'files is empty';
        return false;
    }
    //上传文件
    foreach ($files as &$file) {
        if (!$this->uploadOne($file, $savePath, $type)) {
            return false;
        }
    }
    return $files;
}

/**
 * 单文件上传
 * @param array $file
 * @param array $savePath
 * @param array $type
 * @return bool|array
 */
public function uploadOne(&$file, $savePath, $type)
{

    $saveName = time() . rand(10001, 99998);
    $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
    if (!empty($type)) {
        if (!in_array($ext, $type)) {
            $this->error = 'ext is error';
            return false;
        }
    }
    $file['md5'] = md5_file($file['tmp_name']);
    $file['sha1'] = sha1_file($file['tmp_name']);
    $file['save_path'] = $savePath;
    $file['save_name'] = $saveName . '.' . $ext;
    $file['file_name'] = $file['name'];
    $file['uri'] = $savePath . $saveName . '.' . $ext;
    if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $savePath)) {
        mkdir($_SERVER['DOCUMENT_ROOT'] . $savePath, 0777, true);
    }
    if (!move_uploaded_file($file['tmp_name'], '.' . $savePath . $saveName . '.' . $ext)) {
        $this->error = 'save file error';
        return false;
    }
    return true;
}

/**
 * 批量上传文件数组格式转换
 * @param array $files
 * @return array
 */
protected function convert($files)
{
    $res = array();
    foreach ($files['tmp_name'] as $key => $tmpName) {
        $file['tmp_name'] = $tmpName;
        $file['name'] = $files['name'][$key];
        $file['type'] = $files['type'][$key];
        $file['error'] = $files['error'][$key];
        $file['size'] = $files['size'][$key];
        $res[] = $file;
    }
    return $res;
}

/**
 * 单文件上传、指定文件名
 * @param array $file
 * @param string $name
 * @param string $savePath
 * @param array $type 图片类型
 * @return bool|array
 */
public function uploadFileOne(&$file, $name, $savePath, $type = [])
{

    $saveName = $name;
    $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
    if (!empty($type)) {
        if (!in_array($ext, $type)) {
            $this->error = 'ext is error';
            return false;
        }
    }
    $file['md5'] = md5_file($file['tmp_name']);
    $file['sha1'] = sha1_file($file['tmp_name']);
    $file['save_path'] = $savePath;
    $file['save_name'] = $saveName . '.' . $ext;
    $file['file_name'] = $file['name'];
    $file['uri'] = $savePath . $saveName . '.' . $ext;
    if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $savePath)) {
        mkdir($_SERVER['DOCUMENT_ROOT'] . $savePath, 0777, true);
    }
    if (!move_uploaded_file($file['tmp_name'], '.' . $savePath . $saveName . '.' . $ext)) {
        $this->error = 'save file error';
        return false;
    }
    return true;
}

2.控制器调用方法

public function file()
{

        $savePath ='图片路径';
        $upload = new \App\Admin\Libraries\UploadFile();
        $upload->uploadOne($upFile, $savePath, array('png', 'jpg', 'jpeg', 'bmp', 'gif'));
        $images = $upFile['save_path'] . $upFile['save_name'];
        //返回图片存储路径

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值