File Upload Class _php Foundation

Source: Internet
Author: User
Tags file size file upload

Use examples:
upload.php
<?php
Include_once "upload.class.php";
if ($Submit!= ')
{
$FILEARR [' file '] = $file;
$FILEARR [' name '] = $file _name;
$FILEARR [' size '] = $file _size;
$FILEARR [' type '] = $file _type;
The type of file that/** allows to upload * *
$filetypes = Array (' gif ', ' jpg ', ' jpge ', ' png ');
/** File Upload Directory * *
$savepath = "/usr/htdocs/upload/";
/** no maximum limit of 0 unlimited * *
$maxsize = 0;
/** cover 0 does not allow 1 to allow * *
$overwrite = 0;
$upload = new Upload ($FILEARR, $file _name, $savepath, $filetypes, $overwrite, $maxsize);
if (! $upload->run ())
{
echo "Upload failed". $upload->errmsg ();
}
}
?>
<title> File Upload </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body bgcolor= "#FFFFFF" text= "#000000" >
<form name= "Form1" enctype= "Multipart/form-data" method= "POST" action= "" >
<input type= "File" name= "file" >
<input type= "Submit" name= "submit" value= "Submit" >
</form>
</body>

Upload class
upload.class.php

<?php
//
// +----------------------------------------------------------------------+
// | File Upload
|// | This code is for discussion purposes only, allowing arbitrary modification
|// | AUTHOR:WHXBB (whxbb@21cn.com)
|// +----------------------------------------------------------------------+
//
$Id: Upload.class.php,v 1.0 2001/10/14 14:06:57 whxbb EXP $
//

$UPLOAD _class_error = Array (1 => ' does not allow uploading of this format file ',
2 => ' directory not writable ',
3 => ' file already exists ',
4 => ' unknown error ',
5 => ' file too big '
);

/**
* Purpose
* File Upload
*
* Example
*
$FILEARR [' file '] = $file;
$FILEARR [' name '] = $file _name;
$FILEARR [' size '] = $file _size;
$FILEARR [' type '] = $file _type;
File types that are allowed to upload
$filetypes = Array (' gif ', ' jpg ', ' jpge ', ' png ');
File upload Directory
$savepath = "/usr/htdocs/upload/";
No maximum limit of 0 unrestricted
$maxsize = 0;
Overwrite 0 does not allow 1 allow
$overwrite = 0;
$upload = new Upload ($FILEARR, $file _name, $savepath, $filetypes, $overwrite, $maxsize);
if (! $upload->run ())
{
echo $upload->errmsg ();
}
*
* @author WHXBB (whxbb@21cn.com)
* @version 0.1
*/
Class upload
{
var $file;
var $file _name;
var $file _size;
var $file _type;

/** Save Name * *
var $savename;
/** Save Path * *
var $savepath;
/** file Format Limited */
var $fileformat = array ();
/** Cover Mode * *
var $overwrite = 0;
/** file Maximum byte * *
var $maxsize = 0;
/** file name extension * *
var $ext;
/** Error code *
var $errno;

/**
* Constructor
* @param $fileArr File information array ' file ' temporary file location path and filename
' Name ' upload filename
' Size ' uploads file sizes
' Type ' upload file type
* @param savename File Save name
* @param savepath File Save path
* @param fileformat file format limit array
* @param overwriet coverage 1 allowed to cover 0 prohibit coverage
* @param maxsize file Maximum size
*/
function upload ($FILEARR, $savename, $savepath, $fileformat, $overwrite = 0, $maxsize = 0) {
$this->file = $fileArr [' file '];
$this->file_name = $fileArr [' name '];
$this->file_size = $fileArr [' Size '];
$this->file_type = $fileArr [' type '];

$this->get_ext ();
$this->set_savepath ($savepath);
$this->set_fileformat ($fileformat);
$this->set_overwrite ($overwrite);
$this->set_savename ($savename);
$this->set_maxsize ($maxsize);
}

/** Upload * *
function Run ()
{
/** Check file Format * *
if (! $this->validate_format ())
{
$this->errno = 1;
return false;
}
/** check whether the directory can be written.
if (! @is_writable ($this->savepath))
{
$this->errno = 2;
return false;
}
/** If overwrite is not allowed, check if file already exists * *
if ($this->overwrite = = 0 && @file_exists ($this->savepath. $this->savename))
{
$this->errno = 3;
return false;
}
/** If there is a size limit, check if the file exceeds the limit * *
if ($this->maxsize!= 0)
{
if ($this->file_size > $this->maxsize)
{
$this->errno = 5;
return false;
}
}
/** File Upload * *
if (! @copy ($this->file, $this->savepath. $this->savename))
{
$this->errno = 4;
return false;
}
/** Delete temporary File * *
$this->destory ();
return true;
}

/**
* File Format check
* @access Protect
*/
function Validate_format ()
{

if (!is_array ($this->fileformat))//No Formatting restrictions
return true;
$ext = Strtolower ($this->ext);
Reset ($this->fileformat);
while (the list ($var, $key) = each ($this->fileformat))
{
if (Strtolower ($key) = = $ext)
return true;
}
Reset ($this->fileformat);
return false;
}

/**
* Get the file name extension
* Access Public
*/
function Get_ext ()
{
$ext = Explode (".", $this->file_name);
$ext = $ext [Count ($ext)-1];
$this->ext = $ext;
}
/**
* Set maximum byte limit for upload file
* @param $maxsize File size (bytes) 0: Unlimited
* @access Public
*/
function Set_maxsize ($maxsize)
{
$this->maxsize = $maxsize;
}

/**
* Set Overlay mode
* @param overlay Mode 1: Allow overlay 0: Prohibit overwrite
* @access Public
*/
function Set_overwrite ($overwrite)
{
$this->overwrite = $overwrite;
}

/**
* Set the file format to allow uploading
* @param $fileformat allow uploading of file extensions array group
* @access Public
*/
function Set_fileformat ($fileformat)
{
$this->fileformat = $fileformat;
}

/**
* Set Save Path
* @param $savepath File save path: End With "/"
* @access Public
*/
function Set_savepath ($savepath)
{
$this->savepath = $savepath;
}
/**
* Set File Save name
* @savename save name, if empty, the system automatically generates a random file name
* @access Public
*/
function Set_savename ($savename)
{
if ($savename = = ")//If no filename is set, a random filename is generated
{
Srand (Double) microtime () * 1000000);
$rnd = rand (100,999);
$name = Date (' ymdhis ') + $rnd;
$name = $name. ". $this->ext;
} else {
$name = $savename;
}
$this->savename = $name;
}
/**
* Delete File
* @param $file The file name to be deleted
* @access Public
*/
Function del ($file)
{
if (! @unlink ($file))
{
$this->errno = 3;
return false;
}
return true;
}
/**
* Delete temporary files
* @access Proctect
*/
function Destory ()
{
$this->del ($this->file);
}

/**
* Get error message
* @access Public
* @return Error MSG string or False
*/
function ErrMsg ()
{
Global $UPLOAD _class_error;

if ($this->errno = = 0)
return false;
Else
return $UPLOAD _class_error[$this->errno];
}
}
?>

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.