分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

PHP原生文件上传(单文件多文件均可)简单案例

发布时间:2023-09-06 01:51责任编辑:熊小新关键词:PHP文件上传

本案例共三个文件,目录结构如下图所示:

do_action1.php(前台页面)页面的代码如下:

<?phpheader("content-type:text/html;charset=utf-8;");echo "<pre>";//print_r($_FILES);require_once "upload_function1.php";$files=getFiles();//print_r($files);foreach($files as $fileInfo){ ???$res=uploadFile($fileInfo); ???echo $res[‘mes‘],‘<br/>‘; ???//$uploadFiles[]=@$res[‘dest‘]; ???if(isset($res[‘dest‘])) { ???????$uploadFiles[]=$res[‘dest‘]; ???}else{ ???????continue; ???} }//$uploadFiles=array_values(array_filter($uploadFiles)); 过滤数组中的空值并从新排序print_r($uploadFiles);

upload.html(后台页面)页面的代码如下:

<html><head><meta charset="utf-8"></head><body><form action="do_action1.php" method="post"enctype="multipart/form-data"><!--<input type="hidden" name="MAX_FILE_SIZE" value="176942">--><label for="file">请选择要上传的文件:</label><input type="file" name="file1" id="file" accept="image/jpeg,image/gif,image/png"/> <br /><label for="file">请选择要上传的文件:</label><input type="file" name="file2" id="file" /> <br /><label for="file">请选择要上传的文件:</label><input type="file" name="file[]" id="file" /> <br /><label for="file">请选择要上传的文件:</label><input type="file" name="file[]" id="file" /> <br /><label for="file">请选择要上传的文件:</label><input type="file" name="file[]" id="file" multiple /> <br /><input type="submit" name="submit" value="上传" /></form></body></html>

upload_function1.php(功能函数)页面的代码如下:

<?php//构建上传文件的信息function getFiles(){ ???$i=0; ???foreach($_FILES as $file){ ???????if(is_string($file[‘name‘])){ ???????????$files[$i]=$file; ???????????$i++; ???????}elseif(is_array($file[‘name‘])){ ???????????foreach($file[‘name‘] as $key=>$value){ ???????????????$files[$i][‘name‘]=$file[‘name‘][$key]; ???????????????$files[$i][‘type‘]=$file[‘type‘][$key]; ???????????????$files[$i][‘tmp_name‘]=$file[‘tmp_name‘][$key]; ???????????????$files[$i][‘error‘]=$file[‘error‘][$key]; ???????????????$files[$i][‘size‘]=$file[‘size‘][$key]; ???????????????$i++; ???????????????} ???????} ???} ???return $files;}//得到文件扩展名function getExt($filename){ ???return strtolower(pathinfo($filename,PATHINFO_EXTENSION));}//产生唯一字符串function getUniName(){ ???return md5(uniqid(microtime(true),true));}function uploadFile($fileInfo,$uploadPath="uploads",$maxSize=2097152,$flag=false,$allowExt=array("jpeg","jpg","png","gif")){ ???//$flag=true;//是否检测图片是否是真实的图片类型 ???$allowExt=array(‘jpeg‘,‘jpg‘,‘gif‘,‘png‘); ???if($fileInfo[‘error‘]==UPLOAD_ERR_OK){ ??????//检测上传文件大小 ??????if($fileInfo[‘size‘]>$maxSize){ ??????????$res[‘mes‘]=‘上传文件过大‘; ??????} ??????//检测上传文件的文件类型 ??????$ext=getExt($fileInfo[‘name‘]);//得到上传文件的后缀 ??????if(!in_array($ext,$allowExt)){ ??????????$res[‘res‘]=‘非法文件类型‘; ??????} ??????if($flag){ ??????????if(!getimagesize($fileInfo[‘tmp_name‘])){ ??????????????$res[‘mes‘]=‘不是真实的图片类型‘; ??????????} ??????} ??????//检测文件是否是通过HTTP POST上传上来的 ??????if(!is_uploaded_file($fileInfo[‘tmp_name‘])){ ??????????$res[‘mes‘]=‘文件不是通过HTTP POST方式上传上来的‘; ??????} ??????if(isset($res)) return $res; ??????$path=‘./uploads‘; ??????if(!file_exists($path)){ ??????????mkdir($path,0777,true); ??????????chmod($path,0777); ??????} ??????$uniName=getUniName(); ??????$destination=$path.‘/‘.$uniName.‘.‘.$ext; ??????if(!move_uploaded_file($fileInfo[‘tmp_name‘],$destination)){ ??????????$res[‘mes‘]=$fileInfo[‘name‘].‘文件移动失败‘; ??????} ??????$res[‘mes‘]=$fileInfo[‘name‘].‘上传成功‘; ??????$res[‘dest‘]=$destination; ??????return $res; ???}else{ ???????switch($fileInfo[‘error‘]){ ???????????case 1: ????????????????$res[‘mes‘]="上传文件超出了php配置中upload_max_filesize选项的值"; ???????????????break; ???????????case 2: ????????????????$res[‘mes‘]="超出了表单MAX_FILE_SIZE限制的大小"; ???????????????break; ???????????case 3: ????????????????$res[‘mes‘]="文件部分被上传"; ???????????????break; ???????????case 4: ????????????????$res[‘mes‘]="没有选择上传文件"; ???????????????break; ???????????case 6: ????????????????$res[‘mes‘]="没找到临时目录"; ???????????????break; ???????????case 7: ????????????????$res[‘mes‘]=""; ???????????????break; ???????????case 8: ????????????????$res[‘mes‘]="系统错误"; ???????????????break; ???????} ???????return $res; ???}}

本案例最终可以得到上传成功的图片的存储位置以数组方式存储方便后期与数据库对接,稍微改造即可拿去用了!

PHP原生文件上传(单文件多文件均可)简单案例

原文地址:https://www.cnblogs.com/xiaogou/p/8972243.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved