PHP file upload class PHP file upload code_PHP tutorial

WBOY
Release: 2016-07-13 10:43:27
Original
1095 people have browsed it

A simple PHP file upload class. I found an image processing class when sorting out PHP classes. There are already many classes for PHP to process images, including those that process images separately, some that add watermarks, and some that generate images. In short In PHP, processing images is already very simple. Even in some small applications, the image uploading process can be realized by directly applying PHP's process orientation. Of course, in order to standardize the program, we still need to use PHP's object orientation.

The following PHP file upload class is mainly used to upload files, including pictures, videos, and word documents. In fact, it is recommended to use it to process pictures. The main reason for recommendation is that this class is very standardized and basically all The upload parameters can be defined in the class without modification in php.ini

The code is as follows Copy code

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();

//File saving directory path
$save_path = $path;
//File saving directory URL
$save_url = $path;
//Define the file extensions allowed to be uploaded
$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'),
);
//Maximum file size
$max_size = 1000000;

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

//When files are uploaded
if (empty($_FILES) === false) {
//Original file name
$file_name = $files['name'];
//Temporary file name on the server
$tmp_name = $files['tmp_name'];
//File size
$file_size = $files['size'];
//Directory name
$dir_name = empty($dir) ? 'image' : trim($dir);
//Check file name
if (!$file_name) {
$msg['error'] = "Please select a file.";
}
//Check directory
else if (@is_dir($save_path) === false) {
$msg['error'] = "The upload directory does not exist. Please contact the administrator";
}
//Check directory write permission
else if (@is_writable($save_path) === false) {
$msg['error'] = "The upload directory does not have write permission. Please contact the administrator";
}
//Check if it has been uploaded
else if (@is_uploaded_file($tmp_name) === false) {
$msg['error'] = "The temporary file may not be the uploaded file. Please upload it again";
}
//Check file size
else if ($file_size > $max_size) {
$msg['error'] = "The uploaded file size exceeds the limit.";
}
//Check directory name
else if (empty($ext_arr[$dir_name])) {
$msg['error'] = "The directory name is incorrect.";
}
else
{
//Get file extension
$temp_arr = explode(".", $file_name);
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
//Check extension
if (in_array($file_ext, $ext_arr[$dir_name]) === false) {
$msg['error'] = "The uploaded file extension is not allowed.
n only allows the ". implode(",", $ext_arr[$dir_name]) ." format. ";
}
else
{
//Create folder

$dbsave = ""; //The path stored in the database

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
$new_file_name = date("YmdHis") . '_' . rand(1000
0, 99999) . '.' . $file_ext;
//Move file
$file_path = $save_path . $new_file_name;

if (move_uploaded_file($tmp_name, $file_path) === false) {
$msg['error'] = "Failed to upload file.";
}
//File finally stored in the database
$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;
}//Check extension
}//Directory correctness
return $msg;
}
}
//File upload
}
?>

By using PHP's global array $_FILES, you can upload files from the client computer to a remote server.

The first parameter is the input name of the form, and the second subscript can be "name", "type", "size", "tmp_name" or "error". Like this:

•$_FILES["file"]["name"] - the name of the uploaded file
•$_FILES["file"]["type"] - The type of file being uploaded
•$_FILES["file"]["size"] - The size of the uploaded file, in bytes
•$_FILES["file"]["tmp_name"] - The name of the temporary copy of the file stored on the server
•$_FILES["file"]["error"] - Error code caused by file upload
This is a very simple way to upload files. For security reasons, you should add restrictions on who has permission to upload files.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633163.htmlTechArticleA simple PHP file upload class, an image processing class discovered when sorting out PHP classes, PHP processes images There are already many categories, including those for processing images individually, some for adding watermarks, and...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template