PHP common File Upload class-can upload multiple files

Source: Internet
Author: User
Tags explode file upload save file

<?php

Class my_lib_upfile{

var $upfile, $upfile _name, $upfile _size;
# $upfile temporary filename $_files[' tmp_name '], $upfile _name filename $_files[' name ', $upfile _size file size $_files[' size '];

var $new _upfile_name; # The file name after uploading;
var $fleth, $fileExtent; # File name extension (type);
var $f 1, $f 2, $f 3; # File save path (multilevel) upfiles/2008-01/08/;
var $filename; # file (with path);
var $filepath; #相对路径用来删除文件;
var $maxSize, $File _type; # Allow upload file size to allow the type of file to be uploaded;

var $BuildFile, $newFile, $File _width, $File _height, $rate;

function Upfileclass ($upfile, $upfile _name, $upfile _size) {
$this->upfile = $upfile;
$this->upfile_name = $upfile _name;
$this->upfile_size = $upfile _size;
$this->new_upfile_name = $this->createnewfilename ($this->upfile_name);
$this->f1 = "Public/upload/images";
$this->f2 = $this->f1. " /". Date (' Y ')." -". Date (' m ');
$this->f3 = $this->f2. " /". Date (' d ');
$this->filename = $this->f3. "/" . $this->new_upfile_name;
$this->maxsize = 5000*1024; # File Size 5000KB
$this->file_type = "Gif/jpg/jpeg/png/bmp"; # File types allowed to upload
}

# Create new file name (original file name)
function Createnewfilename ($file _name) {
$this->fleth = Explode (".", $file _name);
$this->fileextent = $this->fleth[(int) count ($this->fleth)-1]; # get file suffix;
$tmps Tutorial tr = date (' YMD '). Rand (0,time ()).    ".". $this->fileextent; # Create a new file name;
return $tmpstr;
}

# Detect file type is correct
function Chk_fileextent () {
$iwTrue = 0;
$fle = Explode ("/", $this->file_type);
For ($i =0 $i < count ($fle); $i + +) {
if ($this->fileextent = = $fle [$i]) {
$iwTrue = (int) $iwTrue + 1;
}
}
if ($iwTrue = = 0) {
$this->msg ("File not compliant". $this->file_type. "Format!");
}
}

# Prompts for error message and terminates operation
function msg ($Error) {
echo "<script language=" JavaScript tutorial ">";
echo ' Alert ('. $Error. ');
echo "Window.history.back ();";
echo "</script>";
Die ();
}

# Save File
function SaveFile () {
$this->chk_fileextent ();
$this->chk_filesize ();
$this->createfolder ("./". $this->f1);
$this->createfolder ("./". $this->f2);
$this->createfolder ("./". $this->f3);
return $this->chk_savefile ();
}

# Detect if upload results are successful
function Chk_savefile () {
$copymsg = Copy ($this->upfile, "./". $this->filename);
if ($copymsg) {
return $this->filename;
}
else{
$this->msg ("File upload failed!") Please upload it again! ");
}
}

# Create a folder
function CreateFolder ($foldername) {
if (!is_dir ($foldername)) {
mkdir ($foldername, 0777);
}
}

# Detect File Size
function Chk_filesize () {
if ($this->upfile_size > $this->maxsize) {
$this->msg ("Destination file cannot be greater than". $this->maxsize/1024. " KB ");
}
}

# Delete file ($filePath file relative path)
function DeleteFile ($filePath) {
if (!is_file ($filePath)) {
return false;
}
else{
$ending = @unlink ($filePath);
return $ending;
}
}

}
?>

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.