Powerful php file upload class
The example in this article shares the PHP file upload class for everyone. It is very powerful and is for your reference. The specific content is as follows
<?PHP /* *文件上传类 **/ class upfile{ private $file_size;//上传源文件大小 private $file_tem;//上传文件临时储存名 private $file_name;//上传文件名 private $file_type;//上传文件类型 private $file_max_size=2000000;//允许文件上传最大 private $file_folder="uploadFiles";//文件上传路径 private $over_write=false;//是否覆盖同名文件 //允许上传图片的类型 private $allow_type=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png'); //构造类,file:上传文件域 function __construct($file){ $this->file_name=$_FILES[$file]['name'];//客户端原文件名 $this->file_type=$_FILES[$file]['type'];//文件类型 $this->file_tem=$_FILES[$file]['tmp_name'];//储存的临时文件名,一般是系统默认 $this->file_size=$_FILES[$file]['size'];//文件大小 } //如果文件夹不存在则创建文件夹 function creatFolder($f_path){ if(!file_exists($f_path)){ mkdir($f_path,0777); } } //判断文件是不是超过上传限制 function is_big(){ if($this->file_size>$this->file_max_size){ echo "文件太大,超过限制!"; exit; } } //检查文件类型 function check_type(){ if(!in_array($this->file_type,$this->allow_type)){ echo "上传文件类型不正确"; exit; } } //检查文件是否存在 function check_file_name(){ if(!file_exists($this->file_tem)){ echo "上传文件不存在"; exit; } } //检查是否有同名文件,是否覆盖 function check_same_file($filename){ if(file_exists($filename)&&$this->over_write!=true){ echo "同名文件已存在!"; exit; } } //移动文件 function move_file($filename,$destination){ if(!move_uploaded_file($filename,$destination)){ echo "移动文件出错"; exit; } } //检查文件是否是通过 HTTP POST 上传的 function is_upload_file(){ if(!is_uploaded_file($this->file_tem)){ echo "文件不存在"; exit; } } //获得文件后缀名 function getext(){ $ext=$this->file_name; $extstr=explode('.',$ext); $count=count($extstr)-1; return $extstr[$count]; } //新建文件名 function set_name(){ return time().".".$this->getext(); } //建立以年月日为文件夹名 function creat_mulu(){ $this->creatFolder("../../upload/".date(Ymd)); return "upload/".date(Ymd); } //生成的文件名 function files_name(){ $name=$this->set_name(); $folder=$this->creat_mulu(); return "../../".$folder."/".$name; } //上传文件 function upload_file(){ $f_name=$this->files_name(); move_uploaded_file($this->file_tem,$f_name); return $f_name; } //生成缩略图 //最大宽:120,高:120 function create_simg($img_w,$img_h){ $name=$this->set_name(); $folder=$this->creat_mulu(); $new_name="../../".$folder."/s_".$name; $imgsize=getimagesize($this->files_name()); switch ($imgsize[2]){ case 1: if(!function_exists("imagecreatefromgif")){ echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!返回"; exit(); } $im = imagecreatefromgif($this->files_name()); break; case 2: if(!function_exists("imagecreatefromjpeg")){ echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!返回"; exit(); } $im = imagecreatefromjpeg($this->files_name()); break; case 3: $im = imagecreatefrompng($this->files_name()); break; case 4: $im = imagecreatefromwbmp($this->files_name()); break; default: die("is not filetype right"); exit; } $src_w=imagesx($im);//获得图像宽度 $src_h=imagesy($im);//获得图像高度 $new_wh=($img_w/$img_h);//新图像宽与高的比值 $src_wh=($src_w/$src_h);//原图像宽与高的比值 if($new_wh<=$src_wh){ $f_w=$img_w; $f_h=$f_w*($src_h/$src_w); }else{ $f_h=$img_h; $f_w=$f_h*($src_w/$src_h); } if($src_w>$img_w||$src_h>$img_h){ if(function_exists("imagecreatetruecolor")){//检查函数是否已定义 $new_img=imagecreatetruecolor($f_w,$f_h); if($new_img){ imagecopyresampled($new_img,$im,0,0,0,0,$f_w,$f_h,$src_w,$src_h);//重采样拷贝部分图像并调整大小 }else{ $new_img=imagecreate($f_w,$f_h); imagecopyresized($new_img,$im,0,0,0,0,$f_w,$f_h,$src_w,$src_h); } }else{ $$new_img=imagecreate($f_w,$f_h); imagecopyresized($new_img,$im,0,0,0,0,$f_w,$f_h,$src_w,$src_h); } if(function_exists('imagejpeg')){ imagejpeg($new_img,$new_name); }else{ imagepng($new_img,$new_name); } imagedestroy($new_img); } //imagedestroy($new_img); return $new_name; } } ?>
The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website. .
For more powerful PHP file upload related articles, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP file upload tutorial: How to use HTML forms to upload files In the process of website development, the file upload function is a very common requirement. As a popular server scripting language, PHP can implement the file upload function very well. This article will introduce in detail how to use HTML forms to complete file uploads. 1. HTML form First, we need to use an HTML form to create a file upload page. In the HTML form, the enctype attribute needs to be set to "multipart/form-

With the continuous development of Internet technology, the file upload function has become an essential part of many websites. In the PHP language, we can handle file uploads through some class libraries and functions. This article will focus on the file upload processing method in PHP. 1. Form settings In the HTML form, we need to set the enctype attribute to "multipart/form-data" to support file upload. The code is as follows: <formaction="upload.

PHP file upload security guide: How to use the $_FILES array to obtain uploaded file information Summary: File upload is one of the common functions in web development. However, incorrect file upload implementation can lead to security vulnerabilities, bringing potential risks to the application. This article will introduce how to use PHP's $_FILES array to safely obtain uploaded file information, and combine it with some code examples to help readers better understand. To set appropriate file upload limits in PHP, we can use the php.ini file to

Tips for handling PHP file upload encoding errors and generating corresponding error prompts. File upload is a very common requirement when developing web applications. When processing PHP file uploads, we often encounter encoding errors. This article will introduce some techniques for handling PHP file upload encoding errors and generating corresponding error prompts. In PHP, file uploads are accessed through the $_FILES global variable. You can obtain the name, size, temporary file path and other information of the uploaded file through $_FILES

How to write a simple file upload and download function through PHP. With the development of Internet technology, file upload and download functions have become one of the necessary functions for many websites. By writing a simple file upload and download function, users can easily upload and download files and improve user experience. This article will introduce how to use PHP to write a simple file upload and download function, and provide specific code examples. 1. Implementation of the file upload function. Create a form. First, create a form on the HTML page for users to upload files.

Summary of PHP file upload methods and frequently asked questions File upload is one of the common functions in web development and can be used for users to upload avatars, files, etc. PHP provides a convenient file upload processing method. This article will introduce the PHP file upload method and a summary of common problems in detail. 1. PHP file upload method HTML form To implement file upload, you need to use an HTML form, in which the enctype attribute needs to be set to "multipart/form-data", so that the browser

With the continuous development of the Internet, more and more websites require file upload and download functions. As an open source server-side scripting language, PHP has a wide range of application scenarios and industry recognition. CMS (Content Management System) is one of our common website types. This article will discuss how to use PHP to develop the file upload and download modules in CMS. 1. File upload module 1. Basic principle of uploading files The basic principle of file upload is to transfer files from the client

How to use PHP to implement a simple online file upload and download system requires specific code examples. In the Internet age, file transfer and sharing are very common needs. Whether you are an individual or a business, you need to handle uploading and downloading files conveniently and quickly. As a powerful server-side scripting language, PHP provides a wealth of functions and tools to easily implement file upload and download functions. This article will introduce how to use PHP to implement a simple online file upload and download system, and provide detailed code examples.
