File Upload class _php tutorial

Source: Internet
Author: User
Examples of Use:
upload.php
Include_once "upload.class.php";
if ($Submit! = ")
{
$FILEARR [' file '] = $file;
$FILEARR [' name '] = $file _name;
$FILEARR [' size '] = $file _size;
$FILEARR [' type '] = $file _type;
/** file types allowed to upload */
$filetypes = Array (' gif ', ' jpg ', ' jpge ', ' png ');
/** File Upload directory */
$savepath = "/usr/htdocs/upload/";
/** no maximum limit 0 Unlimited */
$maxsize = 0;
/** cover 0 Not allowed 1 */
$overwrite = 0;
$upload = new Upload ($FILEARR, $file _name, $savepath, $filetypes, $overwrite, $maxsize);
if (! $upload->run ())
{
echo "Upload failed". $upload->errmsg ();
}
}
?>


<title>File Upload</title>








Upload class
upload.class.php

//
// +----------------------------------------------------------------------+
// | File Upload
|// | This code is for discussion purposes only and allows for 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 = ' do not allow uploading of the 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;
Types of files that are allowed to be uploaded
$filetypes = Array (' gif ', ' jpg ', ' jpge ', ' png ');
File upload Directory
$savepath = "/usr/htdocs/upload/";
No maximum limit 0 Unlimited
$maxsize = 0;
Overwrite 0 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 Limit */
var $fileformat = array ();
/** Overlay Mode */
var $overwrite = 0;
/** file Maximum bytes */
var $maxsize = 0;
/** File extension */
var $ext;
/** Error code */
var $errno;

/**
* Constructor function
* @param $fileArr File information array the path and file name of the ' file ' temporary file
' Name ' upload file name
' 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 overwrite 1 allow override 0 prohibit overlay
* @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 if the directory is writable */
if (! @is_writable ($this->savepath))
{
$this->errno = 2;
return false;
}
/** If overwrite is not allowed, check if the 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 files */
$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 (list ($var, $key) = each ($this->fileformat))
{
if (Strtolower ($key) = = $ext)
return true;
}
Reset ($this->fileformat);
return false;
}

/**
* Get 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 uploaded files
* @param $maxsize File size (bytes) 0: Indicates no limit
* @access Public
*/
function Set_maxsize ($maxsize)
{
$this->maxsize = $maxsize;
}

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

/**
* Set the file formats that are allowed to be uploaded
* @param $fileformat allow uploaded file extensions an 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 the file name is not set, a random file name 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 Files
* @param $file The file name you want to delete
* @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];
}
}
?>

http://www.bkjia.com/PHPjc/314183.html www.bkjia.com true http://www.bkjia.com/PHPjc/314183.html techarticle Example of use: upload.php php include_once "upload.class.php", if ($Submit! = ") {$fileArr [' file '] = $file; $fileArr [' Name '] = $file _name; $FILEARR [' size '] = $file _size; $FILEARR ...

  • 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.