|
php 上传文件并生成缩略图的代码,分为单文件与多文件上传,并可以生成缩略图,确实不错,建议大家参考学习下。
完整代码如下。
魔法映像企业网站管理系统
下载
技术上面应用了三层结构,AJAX框架,URL重写等基础的开发。并用了动软的代码生成器及数据访问类,加进了一些自己用到的小功能,算是整理了一些自己的操作类。系统设计上面说不出用什么模式,大体设计是后台分两级分类,设置好一级之后,再设置二级并选择栏目类型,如内容,列表,上传文件,新窗口等。这样就可以生成无限多个二级分类,也就是网站栏目。对于扩展性来说,如果有新的需求可以直接加一个栏目类型并新加功能操作
*
******************************************************/
function UpLoadFileAll($input='UpPic', $path='upload', $ftype='jpg,gif,png', $fsize=2){
$fileInfo = $_FILES[$input]; //文件信息
if(strrpos($path, '/') < strlen($path)-1) $path .= '/'; //上传文件夹.
$myfile = CreatMyFile($path); //创建文件夹
if($myfile==false) return false;
$Atype = explode(',', $ftype); //文件类型.
$fsize = $fsize*1048576; //(1024*1024==1048576=1M)限文件大小,按字节.
$js = "以下文件上传成功:\\n\\n";
if(is_array($fileInfo["error"])){
foreach ($fileInfo["error"] as $key => $error){
if ($error == 0) {
$name = $fileInfo["name"][$key]; //客户端机器文件的原名称.
$size = $fileInfo["size"][$key]; //上传文件的大小,单位为字节.
$type = $fileInfo["type"][$key]; //上传文件类型.
$tmp_name = $fileInfo["tmp_name"][$key]; //文件被上传后在服务端储存的临时文件名.
$type = MyFileType($type); //检测上传文件类型.
$path = $myfile.MakeFname($type); //文件路径包括文件名.
if(in_array($type, $Atype) && $size<=$fsize){
if(@move_uploaded_file($tmp_name, $path)){
$array[] = $path;
$js .= " ".$name." 上传成功 !\\n";
}
}
}
}
}
echo "";
return $array;
}
/*****************************************************************************
* 重设图片尺寸大小:ResizeImage(原图片路径,缩略图(最大)宽度,缩略图(最大)高度)
* 返回值:
* 失败返回: FLASH.
* 成功返回:缩略图路径.
*****************************************************************************/
function ResizeImage($path, $maxwidth, $maxheight){
$picS = substr($path, -3);
$name = substr($path, 0, strrpos($path, '.')).'_S';
switch($picS){
case 'jpg':
$im = @imagecreatefromjpeg($path);
break;
case 'gif':
$im = @imagecreatefromgif($path);
break;
default:
$im = @imagecreatefrompng($path);
}
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}//end if
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}//end if
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}//end if
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}//end if
}else{
$newim = $im;
}//end if
switch($picS){
case 'jpg':
$PicPath = $name.".jpg";
if(imagejpeg($newim, $PicPath)){
imagedestroy($newim);
return str_replace(array('../','./'), '', $PicPath);
}else{
return false;
}
break;
case 'gif':
$PicPath = $name.".gif";
if(imagegif($newim, $PicPath)){
imagedestroy($newim);
return str_replace(array('../','./'), '', $PicPath);
}else{
return false;
}
break;
default:
$PicPath = $name.".png";
if(imagepng($newim, $PicPath)){
imagedestroy($newim);
return str_replace(array('../','./'), '', $PicPath);
}else{
return false;
}
}//end switch
}//end function
/**************************
* 文件属性 $type = 文件属性
***************************/
function MyFileType($type) {
$type = strtolower($type);
switch($type) {
//OFFICE
case 'application/msword' :
$type = 'doc';
break;
case 'application/vnd.ms-excel':
$type = 'xls';
break;
case 'application/vnd.ms-powerpoint':
$type = 'ppt';
break;
//压缩
case 'application/octet-stream':
$type = 'rar';
break;
//文本
case 'text/plain':
$type = 'txt';
break;
//图片
case 'image/pjpeg':
$type = 'jpg';
break;
case 'image/gif':
$type = 'gif';
break;
case 'image/x-png':
$type = 'png';
break;
case 'image/bmp':
$type = 'bmp';
break;
default :
$type = 'err';
}
return $type; //返回文件类型.
}
/******************
* 创建文件夹(路径)
*******************/
function CreatMyFile($fname=''){
switch($fname){
case '':
break;
default:
if(strrpos($fname, '/') < strlen($fname)-1) $fname .= '/';
}
$fname .= date("Y-m");
if(is_dir($fname)) return $fname.'/';
if(mkdir($fname, 0755)==false) return false;
//if(chmod($fname, 0777)==false) return false;
return $fname.'/';
}
/*****************************
* 生成文件名
* $fname ==> 文件名称
* $ftype ==> 文件类型
*****************************/
function MakeFname($ftype) {
$fname = date("mdHis").'_'.rand(100000, 999999);
return $fname.'.'.$ftype;
}
?> |
0
0
相关文章
php源码成品怎么设置_php源码成品配置与功能设置法【指南】
php在线订单系统源码怎么用_用php在线订单系统源码法
php网站源码怎么在lnmp上用_lnmp用php网站源码指南
php源码怎么看_用编辑器查看PHP源码结构教程【技巧】
陌屿云php加密了怎么解密_用陌屿云解密工具还原加密文件教程【技巧】
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。
115
2025.12.24
拼豆图纸生成器有PixelBeads在线版、BeadGen和“豆图快转”;推荐通过pixelbeads.online或搜索“beadgen free online”直达官网,避开需注册的诱导页面。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。
82
2025.12.24
Yandex官方入口网址是https://yandex.com。用户可通过网页端直连或移动端浏览器直接访问,无需登录即可使用搜索、图片、新闻、地图等全部基础功能,并支持多语种检索与静态资源精准筛选。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。
546
2025.12.24
ES6是JavaScript的根本性升级,引入let/const实现块级作用域、箭头函数解决this绑定问题、解构赋值与模板字符串简化数据处理、对象简写与模块化提升代码可读性与组织性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。
150
2025.12.24
php框架是构建web应用程序的架构,提供工具和功能,以简化开发过程。选择合适的框架取决于项目需求和技能水平。实战案例展示了使用laravel构建博客的步骤,包括安装、创建模型、定义路由、编写控制器和呈现视图。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。
20
2025.12.24
热门下载
相关下载
精品课程
最新文章






