if (!function_exists('upload_image')) {
/**
* 上传单张图片
* @param string $form_name 文件表单名
* @param string $save_dir 保存文件夹名
* @param string $error 错误信息
* @return array 返回结果
*/
function upload_image($form_name = 'file', $save_dir = "", &$error = '')
{
// 获取文件对象
$files = \request()->file($form_name);
// 判断是否有上传的文件
if (!$files) {
$error = "请选择图片";
return false;
}
try {
// 允许上传的后缀
$allowext = 'gif,GIF,jpg,JPG,jpeg,JPEG,png,PNG,bmp,BMP';
// 上传路径
$save_dir = empty($save_dir) ? 'temp' : $save_dir;
if (is_array($files)) {
$data = [];
foreach ($files as $file) {
// 使用验证器验证上传的文件
validate(['file' => [
// 限制文件大小(单位b),这里限制为4M
'filesize' => 10 * 1024 * 1024,
// 限制文件后缀,多个后缀以英文逗号分割
'fileExt' => $allowext,
]])->check(['file' => $file]);
// 上传到本地服务器
$savename = Filesystem::putFile($save_dir, $file);
if ($savename) {
// 拼接路径
$path = str_replace('\\', '/', '/' . $savename);
// $data[] = [
// 'filepath' => $path,
// 'filename' => $file->getOriginalName(),
// 'fileext' => $file->extension(),
// 'filesize' => $file->getSize(),
// ];
$data[] = $path;
}
}
return $data;
} else {
// 使用验证器验证上传的文件
validate(['file' => [
// 限制文件大小(单位b),这里限制为4M
'filesize' => 10 * 1024 * 1024,
// 限制文件后缀,多个后缀以英文逗号分割
'fileExt' => $allowext,
]])->check(['file' => $files]);
// 上传到本地服务器
$savename = Filesystem::putFile($save_dir, $files);
if ($savename) {
// 拼接路径
$path = str_replace('\\', '/', '/' . $savename);
// $data = [
// 'filepath' => $path,
// 'filename' => $files->getOriginalName(),
// 'fileext' => $files->extension(),
// 'filesize' => $files->getSize(),
// ];
return $path;
}
}
} catch (ValidateException $e) {
// 上传校验失败
$error = $e->getMessage();
} catch (Exception $e) {
// 上传异常
$error = $e->getMessage();
}
return false;
}
}
if (!function_exists('upload_file')) {
/**
* 上传单个文件
* @param string $form_name 文件表单名
* @param string $save_dir 存储文件夹名
* @param string $error 错误信息
* @return array 返回结果
*/
function upload_file($form_name = 'file', $save_dir = "", &$error = '')
{
// 获取文件对象
$files = \request()->file($form_name);
// 判断是否有上传的文件
if (!$files) {
$error = "请选择文件";
return false;
}
try {
// 允许上传的后缀
$allowext = 'xls,xlsx,doc,docx,ppt,pptx,zip,rar,mp3,txt,pdf,sql,js,css,chm,';
// 上传路径
$save_dir = empty($save_dir) ? 'temp' : $save_dir;
if (is_array($files)) {
foreach ($files as $file) {
$data = [];
foreach ($files as $file) {
// 使用验证器验证上传的文件
validate(['file' => [
// 限制文件大小(单位b),这里限制为4M
'filesize' => 10 * 1024 * 1024,
// 限制文件后缀,多个后缀以英文逗号分割
'fileExt' => $allowext,
]])->check(['file' => $file]);
// 上传到本地服务器
$savename = Filesystem::putFile($save_dir, $file);
if ($savename) {
// 拼接路径
$path = str_replace('\\', '/', '/' . $savename);
$data[] = [
'fileName' => $file->getOriginalName(),
'filePath' => $path,
];
}
}
return $data;
}
} else {
// 使用验证器验证上传的文件
validate(['file' => [
// 限制文件大小(单位b),这里限制为4M
'filesize' => 10 * 1024 * 1024,
// 限制文件后缀,多个后缀以英文逗号分割
'fileExt' => $allowext,
]])->check(['file' => $files]);
// 上传到本地服务器
$savename = Filesystem::putFile($save_dir, $files);
if ($savename) {
// 拼接路径
$path = str_replace('\\', '/', '/' . $savename);
$result = [
'fileName' => $files->getOriginalName(),
'filePath' => $path,
];
return $result;
}
}
} catch (ValidateException $e) {
// 上传校验失败
$error = $e->getMessage();
} catch (Exception $e) {
// 上传异常
$error = $e->getMessage();
}
}
}
if (!function_exists('formUpload')) {
/**
* 表单提交图片(多图上传)
* @param $name
* @param string $dir
* @param int $width
* @param int $height
* @param int $tooSmall
* @return array
*/
function formUpload($name, $dir = "", $width = 0, $height = 0, &$tooSmall = 0)
{
$allowedExts = array("jpg", "JPG", "jpeg", "JPEG", "gif", "GIF", "png", "PNG", "bmp", "BMP", "tif", "TIF", "svg", "SVG");
$fileData = $_FILES[$name];
$fileList = $fileData['tmp_name'];
if (!$fileList) {
return array();
}
if (!is_array($fileList)) {
$fileList = array($fileList);
$tempData = $fileData;
$fileData = array();
$fileData['error'][0] = $tempData['error'];
$fileData['name'][0] = $tempData['name'];
}
$images = array();
foreach ($fileList as $key => $row) {
if ($fileData['error'][$key] !== 0) {
continue;
}
$tempFile = $row;
$filename = $fileData['name'][$key];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (!in_array($ext, $allowedExts)) {
continue;
}
$imgPath = create_image_path($dir, $ext);
$rs = @move_uploaded_file($tempFile, IMG_PATH . $imgPath);
if ($rs) {
$images[] = $imgPath;
} else {
$realPath = IMG_PATH . $imgPath;
if ($width || $height) {
$imageInfo = getimagesize($realPath);
$imageWidth = $imageInfo['width'];
$heightWidth = $imageInfo['height'];
}
if ($width && $imageWidth < $width) {
$tooSmall = 1;
}
if ($height && $heightWidth < $height) {
$tooSmall = 1;
}
}
}
return $images;
}
}
上传文件函数
相关推荐
-
生成图片
from PIL import Image, ImageColor, ImageDraw, ImageFont, ImageFilterdef create_image_with_text(size, color, text, font_path, font_size, text_color, shadow_color, output_path): """ Create a new image of specified size and color with centered text that has a border and shadow. :param size: A tuple con
-
获取指定目录下的所有图片信息
1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach (
-
Thinkphp各版本的PHP要求
ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3