PHP file Upload class php file upload code _php tutorial

Source: Internet
Author: User
Tags php file upload
A simple php file upload class, in the collation of PHP class found in a picture processing class, PHP processing pictures of the class has been a lot of, there are separate processing pictures, there are watermarks, there are also generated pictures, in short, in PHP, processing pictures is very simple, even in some small applications, direct application PHP process to achieve the image upload process, of course, for the specification of the program, we still need to use the object-oriented PHP

The following PHP file Upload class is mainly used to upload files, including pictures, videos, Word documents, in fact, this is recommended to deal with the picture, the main reason is that this class is very canonical, basically all the upload parameters can be defined in the class, and do not need to modify inside the php.ini

The code is as follows Copy Code

Class files{
/**
* Upload
*
* File 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 Save directory path
$save _path = $path;
File Save Directory URL
$save _url = $path;
Define the file extensions that are 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 uploading a file
if (empty ($_files) = = = False) {
Original file name
$file _name = $files [' name '];
Temporary file name on 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 Catalog
else if (@is_dir ($save _path) = = = False) {
$msg [' error '] = "The upload directory does not exist. Please contact the administrator ";
}
Check Directory Write permissions
else if (@is_writable ($save _path) = = = False) {
$msg [' error '] = "Upload directory does not have write permission. Please contact the administrator ";
}
Check whether you have uploaded
else if (@is_uploaded_file ($tmp _name) = = = False) {
$msg [' error '] = ' temporary file may not be an upload file. Please re-upload ";
}
Check File size
else if ($file _size > $max _size) {
$msg [' error '] = "The upload file size exceeds the limit. ";
}
Check directory Name
else if (empty ($ext _arr[$dir _name])) {
$msg [' error '] = ' directory name is incorrect. ";
}
Else
{
Get file name 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 upload file name extension is not allowed.
n Allow only ". Implode (",", $ext _arr[$dir _name]). Format ";
}
Else
{
Create a 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;
Moving files
$file _path = $save _path. $new _file_name;

if (Move_uploaded_file ($tmp _name, $file _path) = = = = False) {
$msg [' error '] = ' failed to upload the file. ";
}
Files that are eventually stored by 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 a 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". Just like this:

$_files["File" ["Name"]-the name of the file being uploaded
$_files["File" ["type"]-the type of file being uploaded
$_files["File" ["Size"]-the size of the uploaded file, measured 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. Based on security considerations, you should increase the restrictions on what users have permission to upload files.

http://www.bkjia.com/PHPjc/633163.html www.bkjia.com true http://www.bkjia.com/PHPjc/633163.html techarticle a simple php file upload class, in the collation of PHP class found a picture processing class, PHP processing pictures of the class has been a lot of, there are separate processing pictures, there are watermarks, also ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.