PHP Image upload Class

Source: Internet
Author: User
Tags http post

Objective
In the development of php, it is necessary to use the file upload, the packaging of a picture upload class also need to Be.
one, the controller calls
Public Function Upload_file () {
If (is_post) {
If (!empty ($_files[' Filedata ')) {
Import (' org.upload ', common_path);
$upload = new \upload ();
Allow file size to be uploaded
$upload->allowmaxsize (C (' allow_upload_file_max_size '));
Allowed file types to upload
$upload->allowext (C (' Allow_upload_file_type '));
Get information about uploading a file
$upload->get_upload_file_info ($_files[' Filedata ');
Specify upload Directory
$upload->root_dir (root_path);
Generate file name
$file _name = $upload->upload_file_name ();
Save to the specified directory
$res = $upload->save_file (' uploads/', $file _name);
if ($res = = = False) {
Dump ($upload->get_error ());
} else {
Echo ' Upload succeeded ';
}
}
}
}

second, the File Upload class code

<?php

/**
* @desc File Upload class
* @author Timothy
* Created by Phpstorm.
* DATE:2016/10/17
* TIME:12:18
*/
Class Upload
{
protected $_error = ";
Protected $_allowext = Array ();
protected $_allowsize = 0;
protected $file _info = null;
protected $_root_dir = null;

/**
* @desc upload file information
* @param array $file _info
* @return BOOL
*/
Public function Get_upload_file_info (array $file _info = array ()) {
If (!is_uploaded_file ($file _info[' tmp_name ')) {
$this->_error = ' file is not uploaded via HTTP post ';
Return false;
}
$this->file_info = $this->_format_upload_file_info ($file _info);
}

/**
* @desc formatted information for uploading files
* @param array $file _info
* @return Array
*/
Private Function _format_upload_file_info (array $file _info = array ()) {
$pathinfo = pathinfo ($file _info[' name '], pathinfo_extension);
$file _info[' Extension ') = $pathinfo;
return $file _info;
}

Error handling of uploading files
Private Function _checkerror ($file _error = ") {
Switch ($file _error) {
Case Upload_err_ini_size:
$this->_error = ' Uploading the file exceeds the value of the upload_max_filesize selection in the PHP configuration file ';
Break
Case Upload_err_form_size:
$this->_error = ' exceeds the size of the form max_file_size limit ';
Break
Case Upload_err_partial:
$this->_error = ' File part is uploaded ';
Break
Case Upload_err_no_file:
$this->_error = ' No files are uploaded ';
Break
Case Upload_err_no_tmp_dir:
$this->_error = ' Temporary file not found ';
Break
Case Upload_err_cant_write:
$this->_error = ' File Write Failed ';
Break
Case Upload_err_extension:
$this->_error = ' Incorrect file type ';
Break
Default
$this->_error = ' System error ';
Break
}
}

/**
* @desc determine the type of upload file
* @param string $file _type
* @return BOOL
*/
Private Function _checkext ($file _type = ") {
If (!in_array ($file _type, $this->_allowext)) {
$this->_error = ' The file type being uploaded is incorrect ';
Return false;
}
Return true;
}

/**
* @desc determine the size of the uploaded file
* @param int $file _size
* @return BOOL
*/
Private Function _checksize ($file _size = 0) {
If ($file _size > $this->_allowsize) {
$this->_error = ' uploaded image is too large ';
Return false;
}
Return true;
}

/**
* @desc processing of file types that are allowed to upload
* @param int $max _size
* @return Bool|int
*/
Public Function allowmaxsize ($max _size = 2) {
If (!is_numeric ($max _size)) {
$this->_error = ' The file size allowed to upload is incorrect ';
Return false;
}
$this->_allowsize = $max _size * 1024 * 1024;
}

/**
* @desc processing of file types that are allowed to upload
* @param string $ext
*/
Public function Allowext ($ext = ") {
$this->_allowext = Explode (' | ', $ext);
}

/**
* @desc specified path
* @param None
* @return void
*/
Public Function Root_dir ($dir) {
$this->_root_dir = $dir;
}

/**
* @desc Create a directory recursively
* @param $path
* @return BOOL
*/
Private Function _mkdir ($path) {
If (!is_dir ($path)) {
If (@mkdir ($path, 0777, true))
Return true;
Else
$this->_error = ' Directory creation failed ';
Return false;
} else {
Return true;
}
}

Generates a unique file name to prevent overwriting with duplicate names
Public Function Upload_file_name () {
return MD5 (uniqid (microtime (true), true)). ‘.‘ . $this->file_info[' extension '];
}

/**
* @desc Save the uploaded temporary files to the specified directory
* @param string $path
* @param string $file _name
* @return String
*/
Public function Save_file ($path = ', $file _name = ') {
Judging the error number
If ($this->file_info[' error '] > 0) {
If (! $this->_checkerror ($this->file_info[' error ')) {
Return false;
}
}

Determine if the file type is valid
If (! $this->_checkext ($this->file_info[' extension ')) {
Return false;
}

Determine if the file size is legal
If (! $this->_checksize ($this->file_info[' size ')) {
Return false;
}

If (! $this->_checktrueimage ($this->file_info[' tmp_name ')) {
Return false;
}

$abs _path = $this->_root_dir? $this->_root_dir. $path: $path;
If ($this->_mkdir ($abs _path)) {
If (move_uploaded_file ($this->file_info[' tmp_name '), $abs _path. $file _name)) {
@chmod ($abs _path, 0666);
return $abs _path;
}
} else {
$this->_error = ' Upload file failed ';
}
}

/**
* @desc to determine if the picture is true
* @param string $file _info
* @return BOOL
*/
Private Function _checktrueimage ($file _info = ") {
If (!getimagesize ($file _info)) {
$this->_error = ' file is not a real picture ';
Return false;
}
Return true;
}

/**
* @desc Get the upload error message and return
* @return String
*/
Public Function Get_error () {
Return $this->_error;
}
}

PHP Image upload Class

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.