A convenient simple php file upload class

Source: Internet
Author: User
Tags date empty error code explode file upload html form php file php file upload

Previously used the above file upload class, found that there are many inconvenient places.

Now I have to do a new one, everyone help me review, is there any deficiencies.

<?php
/*
* File Upload Class
* Author: More than a rookie
* Contact Email: KINGERQ at MSN DOT com
* Date Created: 2005-06-11
SourceHttp://blog.csdn.net/kingerq
*
Instance
?
$f = new Upfile ("./soft/", "gif,jpg,png");
Upload single or multiple file names with the same file name
if (Isset ($_files["FILES"]) {
if ($f->upload ("files")) {
echo "File upload succeeded."
}
}
Upload multiple different file domain name file
if (Isset ($_files) {
foreach ($_files as $key => $val)
if ($f->upload ($key)) {
echo "File upload succeeded."
}
}
?>
*/
Class upfile{
Uploading file information
var $filename;
Save Name
var $savename;
Save path
var $savepath;
File format Limited, empty without restriction formatting
var $format = "";
Overlay mode
var $overwrite = 0;
/* $overwrite = 0 o'clock do not overwrite file with same name
* $overwrite = 1 o'clock overwrite file with same name
*/
File Maximum bytes
var $maxsize = 210000000;
File name extension
var $ext;
Error code
var $errno = 0;

/* Constructor
* $path Save Path
* $format file format (separated by commas)
* Maximum $maxsize file limit
* $over covering parameters
*/
function Upfile ($path, $format = "", $maxsize = 0, $over = 0) {
$this->savepath = substr ($path,-1) = = "/"? $path: $path. " /";//save path
$this->overwrite = $over;//whether to cover the same name file
$this->maxsize = $maxsize? $this->maxsize: $maxsize;//File Maximum byte
$this->format = $format;
}

/*
* Function: Detect and organize files
* $form file Domain name
* $file upload file to save the name, empty or upload multiple files by the system automatically generated name
*/
function upload ($form, $file = "") {
if (!isset ($_files[$form])) {
$this->halt ("The specified file domain name does not exist.) ");
}else{
$filear = $_files[$form];
}

if (!is_writable ($this->savepath)) {
$this->halt ("The specified path is not writable.) ");
}

if (Is_array ($filear ["name"])) {//upload the same file domain name multiple files
for ($i = 0; $i < count ($filear [name]); $i + +) {
$ar ["name"] = $filear ["name"] [$i];
$ar ["tmp_name"] = $filear ["Tmp_name"] [$i];
$ar ["size"] = $filear ["Size"] [$i];
$ar ["error"] = $filear ["Error"] [$i];

$this->getext ($ar ["name"]);//Get extension
$this->set_savename ();//Set Save file name
$this->copyfile ($ar);
}
}else{//Upload a single file
$this->getext ($filear ["name"]);//Get extension
$this->set_savename ($file);//Set Save file name
$this->copyfile ($filear);
}
return true;
}

/*
* Function: Detect and copy uploaded files
* $filear upload file Data array
*/
function CopyFile ($filear) {

if ($filear [size] > $this->maxsize) {
$this->halt ("Upload file". $filear ["name"]. "The size exceeds the system-qualified value [". $this->maxsize. "bytes], cannot be uploaded. ");
}

if (! $this->overwrite && file_exists ($this->savename)) {
$this->halt ($this->savename.) file name already exists. ");
}

if ($this->format!= "" "&&!in_array (Strtolower ($this->ext), Explode (", ", Strtolower ($this->format)) )){
$this->halt ($this->ext. "File format is not allowed to upload. ");
}

if (!copy ($filear ["Tmp_name"], $this->savepath. $this->savename)) {
$errors = Array (0=> "file upload succeeded",
1=> "The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini. ",
2=> "The size of the upload file exceeds the value specified by the Max_file_size option in the HTML form. ",
3=> "file is only partially uploaded. ",
4=> "No files were uploaded. ");
$this->halt ($errors [$filear ["Error"]]);
}else{
@unlink ($filear ["Tmp_name"]);//Delete temporary files
}
}

/*
* Function: Get the file name extension
* $filename for file name
*/
function Getext ($filename) {
if ($filename = = "") return;

$ext = Explode (".", $filename);

$this->ext = $ext [1];
}

/*
* Function: Set File Save name
* $savename save name, if empty, the system automatically generates a random file name
*/
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 (' U ') + $rnd;
$name = $name. ". $this->ext;
} else {
$name = $savename;
}
$this->savename = $name;
}

/*
* Function: Error hint
* $msg for output information
*/
function Halt ($msg) {
echo "<strong> attention:</strong>". $msg;
Exit
}
}
?>



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.