Home php教程 php手册 php文件上传类 php文件上传代码

php文件上传类 php文件上传代码

Jun 13, 2016 am 09:46 AM
php upload code Discover picture exist deal with tidy document of Simple kind

一个简单的php文件上传的类,在整理 php 类时发现的一个图片处理类,PHP 处理图片的类已经很多了,有单独处理图片的,也有加水印的,也有生成图片的,总之,在 PHP 当中,处理图片已经很简单了,甚至在一些小型的应用中,直接应用 PHP 的面向过程就可以实现图片的上传过程,当然,为了程序的规范,我们还是有必要使用PHP的面向对象的

下面的这个PHP文件的上传类主要是用来上传文件的,包括图片,视频,word文档的,其实这里建议用来处理图片,推荐的主要原因是这个类很规范,基本上所有的上传参数都可以在类里面进行定义,而不需要在 php.ini 里面进行修改

 代码如下 复制代码

class files{
/**
* upload
*
* 文件上传
*
* @param String $path e.g. Zend_Registry::get('upload')
* @param Array $files e.g. $_FILES['Filedata']
* @param String $dir e.g. $_POST['dir']
*
* return Array $msg e.g. if($msg['error'])
*/
static function upload($path,$files,$dir)
{
$msg=array();

//文件保存目录路径
$save_path = $path;
//文件保存目录URL
$save_url = $path;
//定义允许上传的文件扩展名
$ext_arr = array(
'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
'flash' => array('swf', 'flv'),
'media' => array('swf', 'flv', 'mp3', 'wav', 'wm
a', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
'file' => array('doc', 'docx', 'xls', 'xlsx', 'pp
t', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
);
//最大文件大小
$max_size = 1000000;

$save_path = realpath($save_path) . '/';

//有上传文件时
if (empty($_FILES) === false) {
//原文件名
$file_name = $files['name'];
//服务器上临时文件名
$tmp_name = $files['tmp_name'];
//文件大小
$file_size = $files['size'];
//目录名
$dir_name = empty($dir) ? 'image' : trim($dir);
//检查文件名
if (!$file_name) {
$msg['error'] = "请选择文件。";
}
//检查目录
else if (@is_dir($save_path) === false) {
$msg['error'] = "上传目录不存在。请联系管理员";
}
//检查目录写权限
else if (@is_writable($save_path) === false) {
$msg['error'] = "上传目录没有写权限。请联系管理员";
}
//检查是否已上传
else if (@is_uploaded_file($tmp_name) === false) {
$msg['error'] = "临时文件可能不是上传文件。请重新上传";
}
//检查文件大小
else if ($file_size > $max_size) {
$msg['error'] = "上传文件大小超过限制。";
}
//检查目录名
else if (empty($ext_arr[$dir_name])) {
$msg['error'] = "目录名不正确。";
}
else
{
//获得文件扩展名
$temp_arr = explode(".", $file_name);
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
//检查扩展名
if (in_array($file_ext, $ext_arr[$dir_name]) === false) {
$msg['error'] = "上传文件扩展名是不允许的扩展名。
n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。";
}
else
{
//创建文件夹

$dbsave = ""; //数据库中存放的路径

if ($dir_name !== '') {
$save_path .= $dir_name . "/";
$save_url .= $dir_name . "/";
$dbsave = $dir_name.'/';
if (!file_exists($save_path)) {
mkdir($save_path);
}
}

$y = date("Y");
$m = date("m");
$d = date("d");

$save_path .= $y . "/";
$save_url .= $y . "/";
$dbsave .= $y.'/';
if (!file_exists($save_path)) {
mkdir($save_path);
}

$save_path .= $m . "/";
$save_url .= $m . "/";
$dbsave .= $m.'/';
if (!file_exists($save_path)) {
mkdir($save_path);
}

$save_path .= $d . "/";
$save_url .= $d . "/";
$dbsave .= $d.'/';
if (!file_exists($save_path)) {
mkdir($save_path);
}

//新文件名
$new_file_name = date("YmdHis") . '_' . rand(1000
0, 99999) . '.' . $file_ext;
//移动文件
$file_path = $save_path . $new_file_name;

if (move_uploaded_file($tmp_name, $file_path) === false) {
$msg['error'] = "上传文件失败。";
}
//数据库最终存储的文件
$dbsave .= $new_file_name;
@chmod($file_path, 0644);
$file_url = $save_url . $new_file_name;
$msg['file_url'] = $file_url;
$msg['file_size'] = $file_size;
$msg['db_path'] = $dbsave;
}//检查扩展名
}//目录正确性
return $msg;
}
}
//文件上传
}
?>

通过使用 PHP 的全局数组 $_FILES,你可以从客户计算机向远程服务器上传文件。

第一个参数是表单的 input name,第二个下标可以是 "name", "type", "size", "tmp_name" 或 "error"。就像这样:

•$_FILES["file"]["name"] - 被上传文件的名称
•$_FILES["file"]["type"] - 被上传文件的类型
•$_FILES["file"]["size"] - 被上传文件的大小,以字节计
•$_FILES["file"]["tmp_name"] - 存储在服务器的文件的临时副本的名称
•$_FILES["file"]["error"] - 由文件上传导致的错误代码
这是一种非常简单文件上传方式。基于安全方面的考虑,您应当增加有关什么用户有权上传文件的限制。

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks 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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles