PHP file upload code

Source: Internet
Author: User
Tags php file upload

A simple php File Upload class is an image processing class found during PHP class sorting. php has many image processing classes, including processing images separately and adding watermarks, there are also images generated. In short, it is very easy to process images in PHP. Even in some small applications, you can directly use the PHP-oriented process to upload images, of course, for the sake of program specification, we still need to use PHP's object-oriented

The following PHP File Upload class is mainly used to upload files, including images, videos, and Word documents. In fact, it is recommended to process images. The main reason for recommendation is that this class is very standard, basically, all upload parameters can be defined in the class, instead of in php. ini.

The Code is as follows: Copy code

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

// Path of the file storage directory
$ Save_path = $ path;
// File Save directory URL
$ Save_url = $ path;
// Define the file extension that can 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', 'av', '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 a file is 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 the file name
If (! $ File_name ){
$ Msg ['error'] = "select a file. ";
}
// Check the Directory
Else if (@ is_dir ($ save_path) === false ){
$ Msg ['error'] = "the upload directory does not exist. Contact the Administrator ";
}
// Check the directory write permission
Else if (@ is_writable ($ save_path) === false ){
$ Msg ['error'] = "the upload directory has no write permission. Contact the Administrator ";
}
// Check whether the object has been uploaded
Else if (@ is_uploaded_file ($ tmp_name) === false ){
$ Msg ['error'] = "temporary files may not be uploaded. Please upload again ";
}
// Check the file size
Else if ($ file_size> $ max_size ){
$ Msg ['error'] = "the size of the uploaded file exceeds the limit. ";
}
// Check the directory name
Else if (empty ($ ext_arr [$ dir_name]) {
$ Msg ['error'] = "the directory name is incorrect. ";
}
Else
{
// Obtain the file extension
$ Temp_arr = explode (".", $ file_name );
$ File_ext = array_pop ($ temp_arr );
$ File_ext = trim ($ file_ext );
$ File_ext = strtolower ($ file_ext );
// Check the extension
If (in_array ($ file_ext, $ ext_arr [$ dir_name]) === false ){
$ Msg ['error'] = "the File Upload extension is not allowed.
N only supports the ". implode (", ", $ ext_arr [$ dir_name])." format. ";
}
Else
{
// Create a folder

$ Dbsave = ""; // 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 the file
$ File_path = $ save_path. $ new_file_name;

If (move_uploaded_file ($ tmp_name, $ file_path) === false ){
$ Msg ['error'] = "An error occurred while uploading the file. ";
}
// The final storage file of 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 the extension
} // Directory correctness
Return $ msg;
}
}
// File Upload
}
?>

By using the Global Array $ _ FILES of PHP, 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"]-name of the uploaded file
• $ _ FILES ["file"] ["type"]-type of the file to be uploaded
• $ _ FILES ["file"] ["size"]-size of the uploaded file, in bytes
• $ _ FILES ["file"] ["tmp_name"]-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 File Upload method. Based on security considerations, you should add restrictions on which users have the right to upload files.

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.