search
Homephp教程PHP源码PHP手写的图片上传类

图片上传网上只要你愿意搜索多了去了,但有几个站长会是自己写的无非就是抄来抄去了,下面我来给大家抄一个比较好用的,自己也测试过的图片上传代码了,希望这个类对各位会带来帮助。

<script>ec(2);</script>

一个新手写的 PHP 图片上传类,基本的功能已经满足,可以返回客户端文件名,文件大小,临时文件夹路径,允许上传的文件类型,文件后缀,新路径,新文件名,图片宽度,图片高度,自定义上传成功消息,显示上传的状态。。是否上传成功的信息等等,外加构造函数,对于新手学习来说,已经足够了,值得研究学习。。

在使用时,和其它PHP类一样,先要进行初始化对象,在前台调用函数方法即可,这里不再详细说明,这个类的研究价值主要在于代码的规范与实用,并没有大量繁琐的代码,都是一些简单的上传函数整合成的一个PHP图片上传类,下面是详细的类代码:

 代码如下 复制代码

class upload {
private $file_name = ‘null’; #客户端文件名
private $file_size = 0; #文件大小
private $file_tmpName = ”; #临时文件夹路径
private $upload_type = array(‘jpg’,'jpeg’,'gif’,'png’); #允许上传的文件类型
private $upload_Ext = ‘null’; #文件后缀
private $upload_dir = ‘null’; #新路径
private $upload_name = ‘null’; #新文件名
private $upload_width = 0; #图片宽度
private $upload_height = 0; #图片高度
private $upload_msg = ”; #上传消息
private $upload_mode; #上传状态
/**
* 构造函数,自动上传
* @access public
* @param string $control 文件域控件名
* @param string $filePath 保存路径
* @param string $fileName 新文件名,不带后缀
* @param array $fileType 允许上传的文件后缀
* @param int $size 允许上传的文件大小,以字节为单位
*/
public function __construct($control=”,$filePath=”,$fileName=”,$fileType=”,$size=0) {
$this->upload_mode = true;
if($control==”)
$this->error(‘上传的文件域控件名不能为空’);
else{
$this->file_name=$_FILES[$control]['name'];
$this->file_size=$_FILES[$control]['size'];
$this->file_tmpName=$_FILES[$control]['tmp_name'];
if($fileType!=”)
$this->upload_type = $fileType;
if($filePath==”)
$this->upload_dir=’./’;
else
$this->upload_dir=$filePath;
if(!is_uploaded_file($this->file_tmpName))
$this->error(‘没有文件被上传’);
else{
if($size!=0&&$this->file_size>$size)
$this->error(‘上传的图片文件不能大于’.$this->getSize($size));
if(!$this->typeDetect($this->file_name))
$this->error(‘不支持上传此类型的文件’);
else{
if(!$this->fileDetect($this->file_tmpName))
$this->error(‘上传的图片文件无效或已经损坏’);
}
if($fileName==”)
$this->upload_name=$this->file_name;
else
$this->upload_name=$fileName.’.’.$this->upload_Ext;
}
}
//开始保存文件
if($this->mode)
$this->saveFile();
}
/**
* 获取属性
* @access public
* @param string 属性名
* @return 属性值
*/
public function __get($name){
switch($name){
case ‘path’:
return $this->upload_dir;
break;
case ‘name’:
return $this->upload_name;
break;
case ‘ext’:
return $this->upload_Ext;
break;
case ‘size’:
return $this->getSize($this->file_size);
case ‘width’:
return $this->upload_width;
break;
case ‘height’:
return $this->upload_height;
break;
case ‘mode’:
return $this->upload_mode;
break;
case ‘msg’:
return $this->upload_msg;
break;
}
}
/**
* 检测文件后缀名
* @access public
* @return bool
*/
private function typeDetect($OldFile){
$tempArr = explode(“.”, $OldFile);
$fileExt = array_pop($tempArr);
$fileExt = trim($fileExt);
$fileExt = strtolower($fileExt);
if(in_array($fileExt,$this->upload_type)){
$this->upload_Ext=$fileExt;
return true;
}
else
return false;
}
/**
* 检测上传文件是否是有效图片格式
* @access private
* @return bool
*/
private function fileDetect($tmpName){
$arr = getimagesize($tmpName);
if(!$arr){
return false;
}else{
$this->upload_width = $arr[0];
$this->upload_height = $arr[1];
return true;
}
}
/**
* 保存文件
* @access private
*/
private function saveFile(){
if(!is_dir($this->upload_dir)){
mkdir($this->upload_dir, 0777);
chmod($this->upload_dir, 0777);
}
$save_file = move_uploaded_file($this->file_tmpName,$this->upload_dir.$this->upload_name);
if(!$save_file)
$this->error(‘文件写入失败’);
else
$this->upload_msg=’文件上传成功’;
}
/**
* 获取文件大小
* @return string 返回B,KB,MB单位
*/
private function getSize($tmpSize){
$value = ‘B’;
if($tmpSize>1024){
$tmpSize = floor($tmpSize/1024);
$value = ‘KB’;
}
if($tmpSize>1024){
$tmpSize = round($tmpSize/1024,2);
$value = ‘MB’;
}
return $tmpSize.$value;
}
/**
* 错误提示
* @access private
* @param string $msg 错误信息
*/
private function error($msg=”){
$this->upload_msg .= $msg;
$this->upload_mode = false;
}
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.