ONE'S 博客

  • 首页
  • 生活
  • 工作
  • 爱好
    • C #
    • javascript
    • Linux
    • PHP编程
    • python
    • thinkphp
    • 旅游风景
    • 电脑网络
    • 脑力运动
    • 瞎编乱造
  • 书籍
  • 经验
  • 健康
  • 交易
  • 时间
  • 作品
  • 小说
ONE'S BLOG
弱水三千 只取一瓢
  1. 首页
  2. 爱好拓展
  3. PHP编程
  4. 正文

php图片上传

2019年8月7日 689点热度 0条评论

具体实现:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="Action.php" method="post" enctype="multipart/form-data">
    请选择您要上传的文件:<input type="file" name='myFile1' />
    <input type="submit" value="上传文件" />
</form>
</body>
</html>

action.php

<?php
header('content-type:text/html;charset=utf-8');
require_once 'upload.php';
$upload=new upload('myFile1','files');
$dest=$upload->uploadFile();
echo $dest;

upload.php

<?php
class upload{
protected $fileName;			//文件名
protected $maxSize;			//文件最大值
protected $allowMime;		//允许的文件类型
protected $allowExt;			//允许的文件名
protected $uploadPath;		//上传路径
protected $imgFlag;			//检测是否真实图片文件
protected $fileInfo;				//文件信息数组
protected $error;					//检测上传文件是否出错
protected $ext;					//扩展名变量
 
/**
* @param string $fileName
* @param string $uploadPath
* @param string $imgFlag
* @param number $maxSize
* @param array $allowExt
* @param array $allowMime
*/
public function __construct($fileName='myFile',$uploadPath='./uploads',$imgFlag=true,$maxSize=5242880,$allowExt=array('jpeg','jpg','png','gif'),$allowMime=array('image/jpeg','image/png','image/gif')){
$this->fileName=$fileName;
$this->maxSize=$maxSize;
$this->allowMime=$allowMime;
$this->allowExt=$allowExt;
$this->uploadPath=$uploadPath;
$this->imgFlag=$imgFlag;
$this->fileInfo=$_FILES[$this->fileName];
}
 
/**
* 检测上传文件是否出错
* @return boolean
*/
protected function checkError(){
if(!is_null($this->fileInfo)){
if($this->fileInfo['error']>0){
switch($this->fileInfo['error']){
case 1:
$this->error='超过了PHP配置文件中upload_max_filesize选项的值';
break;
case 2:
$this->error='超过了表单中MAX_FILE_SIZE设置的值';
break;
case 3:
$this->error='文件部分被上传';
break;
case 4:
$this->error='没有选择上传文件';
break;
case 6:
$this->error='没有找到临时目录';
break;
case 7:
$this->error='文件不可写';
break;
case 8:
$this->error='由于PHP的扩展程序中断文件上传';
break;
 
}
return false;
}else{
return true;
}
}else{
$this->error='文件上传出错';
return false;
}
}
/**
* 检测上传文件的大小
* @return boolean
*/
protected function checkSize(){
if($this->fileInfo['size']>$this->maxSize){
$this->error='上传文件过大';
return false;
}
return true;
}
/**
* 检测扩展名
* @return boolean
*/
protected function checkExt(){
$this->ext=strtolower(pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION));  //pathinfo() 函数返回关于文件路径的信息,[extension]:返回文件路径中文件的类型的部分
if(!in_array($this->ext,$this->allowExt)){
$this->error='不允许的扩展名';
return false;
}
return true;
}
/**
* 检测文件的类型
* @return boolean
*/
protected function checkMime(){
if(!in_array($this->fileInfo['type'],$this->allowMime)){
$this->error='不允许的文件类型';
return false;
}
return true;
}
/**
* 检测是否是真实图片
* @return boolean
*/
protected function checkTrueImg(){
if($this->imgFlag){
if(!@getimagesize($this->fileInfo['tmp_name'])){
$this->error='不是真实图片';
return false;
}
return true;
}
}
/**
* 检测是否通过HTTP POST方式上传上来的
* @return boolean
*/
protected function checkHTTPPost(){
if(!is_uploaded_file($this->fileInfo['tmp_name'])){
$this->error='文件不是通过HTTP POST方式上传上来的';
return false;
}
return true;
}
/**
*显示错误
*/
protected function showError(){
exit('<span style="color:red">'.$this->error.'</span>');
}
/**
* 检测目录不存在则创建
*/
protected function checkUploadPath(){
if(!file_exists($this->uploadPath)){
mkdir($this->uploadPath,0777,true);
}
}
/**
* 产生唯一字符串作为文件名
* @return string
*/
protected function getUniName(){
return md5(uniqid(microtime(true),true));
}
/**
* 上传文件
* @return string
*/
public function uploadFile(){
if($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->checkMime()&&$this->checkTrueImg()&&$this->checkHTTPPost()){
$this->checkUploadPath();
$this->uniName=$this->getUniName();
$this->destination=$this->uploadPath.'/'.$this->uniName.'.'.$this->ext;
if(@move_uploaded_file($this->fileInfo['tmp_name'], $this->destination)){
return  "上传成功,".$this->destination;
}else{
$this->error='文件移动失败';
$this->showError();
}
}else{
$this->showError();
}
}
}

标签: 图片处理
最后更新:2019年8月7日

飞翔的mouse

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

飞翔的mouse

这个人很懒,什么都没留下

分类
  • C #
  • javascript
  • Linux
  • PHP编程
  • python
  • thinkphp
  • 书籍摘录
  • 健康医药
  • 工作感悟
  • 投资理财
  • 旅游风景
  • 时间管理
  • 未分类
  • 生活情感
  • 电脑网络
  • 瞎编乱造
  • 经验分享
  • 脑力运动
  • 软件作品
最新 热点 随机
最新 热点 随机
99%是钱的问题 有些惆怅和反复 处理几个表格 抖狗视频音乐下载工具 聚焦小程序开发 关于windows照片查看器显示内存不足
一种说法 一种释放 散文背诵 《人生,若只如初见》 好想安稳 刘磊博客到连云港教育博客单篇文章转移工具 淘生活,梦一样 微信小程序动态添加和删除组件的现实
最近评论
飞翔的mouse 发布于 11 个月前(11月12日) 好像没!!!
涛涛 发布于 11 个月前(11月11日) 中了没有?
daxi 发布于 5 年前(11月04日) 学无止境,认真拜读!
飞翔的mouse 发布于 6 年前(06月15日) 在手机浏览的情况下,文章内的图片拉伸我的站有问题,你的站没有。求解决方案呀MJ
M.J 发布于 6 年前(06月15日) @飞翔的mouse 哈哈哈哈,谢谢支持,我这程序猿
标签聚合
从业资格 学习 计划 工作 学习计划 会计证 生活 php

COPYRIGHT © 2023 ONE'S 博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

苏ICP备11089789号-4

苏公网安备 320722020186