Home php教程 PHP源码 PHP手写的图片上传类

PHP手写的图片上传类

Jun 08, 2016 pm 05:21 PM
private this

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

<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 of this Website
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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does private mean in java What does private mean in java Nov 24, 2022 pm 06:27 PM

In Java, private means "private" and is an access control modifier used to modify classes, properties and methods. Class members modified with private can only be accessed and modified by the methods of the class itself, and cannot be accessed and referenced by any other class (including subclasses of the class); therefore, the private modifier has the highest level of protection.

Detailed explanation of private access modifiers for Java functions Detailed explanation of private access modifiers for Java functions Apr 25, 2024 pm 04:48 PM

Private is a Java access modifier that limits the accessibility of a function to only the class in which it is defined, including: the function cannot be accessed in other classes. The function is also not accessible in subclasses.

An article that understands this point and catches up with 70% of front-end people An article that understands this point and catches up with 70% of front-end people Sep 06, 2022 pm 05:03 PM

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.

Let's talk about why Vue2 can access properties in various options through this Let's talk about why Vue2 can access properties in various options through this Dec 08, 2022 pm 08:22 PM

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

Clever way to use this keyword in jQuery Clever way to use this keyword in jQuery Feb 25, 2024 pm 04:09 PM

Flexible use of this keyword in jQuery In jQuery, the this keyword is a very important and flexible concept. It is used to refer to the DOM element currently being manipulated. By rationally using this keyword, we can easily operate elements on the page and achieve various interactive effects and functions. This article will combine specific code examples to introduce the flexible use of this keyword in jQuery. Simple this example First, let's look at a simple this example. Suppose we have a

How to use this method in Java How to use this method in Java Apr 18, 2023 pm 01:58 PM

1. this keyword 1. Type of this: Which object is called is the reference type of that object 2. Usage summary 1. this.data;//Access attribute 2. this.func();//Access method 3.this( );//Call other constructors in this class 3. Explanation of usage 1.this.data is used in member methods. Let us see what will happen if this is not added classMyDate{publicintyear;publicintmonth;publicintday; publicvoidsetDate(intyear,intmonth,intday){ye

What is this? An in-depth analysis of this in JavaScript What is this? An in-depth analysis of this in JavaScript Aug 04, 2022 pm 05:02 PM

What is this? The following article will introduce you to this in JavaScript, and talk about the differences between this in different calling methods of functions. I hope it will be helpful to you!

Java programs display different access levels Java programs display different access levels Aug 19, 2023 pm 10:09 PM

Access modifiers are used to set the feature of visibility of some particular classes, interfaces, variables, methods, constructors, data members, and the setter methods in Java programming language. In Java environment, we have different types of access modifiers. Default - If we declare a function, it will only be visible in a specific package. Private- If we declare a function, it will be available only in a specific class

See all articles