File Upload-Terminator Ⅰ

Source: Internet
Author: User
Tags format empty file size file upload sql mysql sprintf variable
Upload | Terminator PHP code:--------------------------------------------------------------------------------

?
/*
------------------------------------------------------------------------------------
Class Name: Lwguploadandquery
Description: SQL statement execution and file upload class, is a subclass of the Lwgupload class
Author: Long Weiguo
Network user:lwg888
Email: lwg888@163.com
Use, modify, propagate please keep author information
------------------------------------------------------------------------------------
*/

Require_once (DirName (__file__). " /lwgdb.inc.php ");//database connection and query class
Require_once (DirName (__file__). " /lwgupload.inc.php ");//Parent class
Class Lwguploadandquery extends lwgupload{
var $sql;//sql statement
var $url;//URL to turn to after file upload succeeded

IMPORTANT NOTE: SQL statement writing:
$sql =sprintf ("Update test1 set name= '%s ', image1= '%s ', image2= '%s ' where id= '%d '", $_post[' name '], "image1_name", " Image2_name ", $_post[' id ']);
In "Image1_name", "Image2_name", Image1 and Image2 are the name or ID of the file field, followed by the "_name" suffix, which makes it easier to find it below and modify it
function Lwguploadandquery ($sql = "", $uploadfield = "", $url = "", $uploadpath = "", $maxsize = "", $ftype = "All") {
$this->sql= $sql;
$this->url= $url;
$this->lwgupload ($uploadfield, $uploadpath, $maxsize, $ftype);
}

function Run () {
if (Empty ($this->sql)) return $this->output ("There is no executable SQL statement. ");

if (!is_array ($this->uploadfield)) $this->uploadfield=array ($this->uploadfield);//If it is not an array, it is easy to simplify the following code
if (!is_array ($this->maxsize)) $this->maxsize=array ($this->maxsize);/ibid.
if (!is_array ($this->ftype)) $this->ftype=array ($this->ftype);/ibid.

$totaluploadfield =count ($this->uploadfield);
for ($i =0; $i < $totaluploadfield; $i + +) {
$uploadfile =$_files[$this->uploadfield[$i]][' tmp_name ');
$file _name=$_files[$this->uploadfield[$i]][' name ');
$file _old=$_post[$this->uploadfield[$i]. ' _old '];//is used in the update to indicate the original file corresponding to the uploaded file
$file _del=$_post[$this->uploadfield[$i]. ' _del '];//whether the original file is marked for deletion

if ($uploadfile!= "" && $file _name!= "") {
Indicates the need to upload, that is, the file field filled with any characters
$uploadfield []= $this->uploadfield[$i];
Get the new upload field because no characters are filled in the file field without uploading
$maxsize []=!empty ($this->maxsize[$i])? $this->maxsize[$i]: $this->maxsize[0];
Get the corresponding size limit value
$ftype []=!empty ($this->ftype[$i])? $this->ftype[$i]: $this->ftype[0];
Gets the corresponding type limit value
$newfile = $this->uploadpath. " /". $file _name;
Post-uploaded file address
$insertname = $file _name;
To change the SQL language, use the value of the corresponding field in the MySQL table to the current upload file name
if ($file _old!= "") $oldfile []= $this->uploadpath.] /". sprintf ('%s ', $file _old);
Used in the update, get the original file address, when the new file upload delete old files
}
else if ($file _old!= "") {
does not need to upload, that is, the file domain does not fill in any characters, but the old file
if ($file _del== "true") {
If the old file is marked for deletion
$insertname = "";
To use when modifying the SQL language, change the value of the corresponding field in the MySQL table to empty
$oldfile []= $this->uploadpath. " /". sprintf ('%s ', $file _old);
}
else $insertname =sprintf ('%s ', $file _old);
Neither upload nor delete, modify the SQL language to use, the MySQL table in the corresponding field of the value of the original file name
}
else $insertname = "";
Neither upload nor original files, modify the SQL language to use, the MySQL table in the corresponding field value to empty
$this->sql=str_replace ($this->uploadfield[$i]. " _name ", $insertname, $this->sql);
Modify SQL statements
}

$this->uploadfield= $uploadfield;
$this->maxsize= $maxsize;
$this->ftype= $ftype;

if (count ($uploadfield) >0) {
if (! $this->test ($oldfile)) return false;
}
Test can upload all

if (count ($oldfile) >0) $this->err_del ((Count ($oldfile)-1), $oldfile);
Delete the original file marked for deletion after passing the test

$db =new lwgdb ();//database connection and query class
if (count ($uploadfield) ==0) {
if (! $db->query ($this->sql)) {
Return $this->output (Error: NO1, commit failed, please try again. ");
NO1 indicates that no file was uploaded and that the SQL statement failed to execute
}
else $this->debugstr.= "SQL statement: '". $this->sql. "' Implementation success <br> ";
}
else {
if ($this->upload ()) {
if (! $db->query ($this->sql)) {
$this->err_del (Count ($uploadfield)-1, $this->newfile);
Delete uploaded files
Return $this->output (Error: NO2, commit failed, please try again. ");
NO1 indicates that the SQL statement failed to execute after the file was uploaded
}
else $this->debugstr.= "SQL statement: '". $this->sql. "' Implementation success <br> ";
}
}
if (!empty ($this->url)) header ("Location:". $this->url);
}
}

/*
--------How to use------------------------------------------------
$uploadname =array ("Image1", "Image2");
$sql =sprintf ("Update test1 set name= '%s ', image1= '%s ', image2= '%s ' where id= '%d '", $_post[' name '), $uploadname [0]. ' _name ', $uploadname [1]. ' _name ', $id);
$obj =new lwguploadandquery ($sql, $uploadname, ', ' Upload ', Array (40960,2048), Array (' jpg,gif ', ' all '));
$obj->run ();
Or:
$obj =new lwguploadandquery ($sql, $uploadname, ', ' Upload ', Array (40960,2048), Array (' jpg,gif ', ' all '));
$obj->uploadname=array ("Image1", "Image2");
$obj->sql=sprintf ("Update test1 set name= '%s ', image1= '%s ', image2= '%s ' where id= '%d '", $_post[' name '), $uploadname [ 0]. ' _name ', $uploadname [1]. ' _name ', $id);
$obj->url= "http://xxxxxx.com";
$obj->uploadpath= "Upload/image";
$obj->maxsize=1024*5;
$obj->ftype= ' SWF ';
$obj->run ();
----------------------------------------------------------------
*/
?>

--------------------------------------------------------------------------------

?
/*
------------------------------------------------------------------------------------
Class Name: Lwgupload
Description: Multi-File Upload class
Author: Long Weiguo
Network user:lwg888
Email: lwg888@163.com
Use, modify, propagate please keep author information
------------------------------------------------------------------------------------
*/

Class lwgupload{
-------------A variable that can set a value------------------------
var $uploadfield;//The field name of the uploaded file
var $maxsize;//Limit the size of the uploaded file
var $file _old;//old files that need to be deleted
var $uploadpath;//File Upload path
var $ftype;//Limit the type of upload file
var $debug =true;//whether to display debugging or error messages

------------the variable used to get the value---------------------------
var $uploadfile//Post-uploaded temporary files
var $file _name;//file name
var $file the size of the _size;//file
var $file _size_format;//formatted $file_size
var $file _type;//File type
var $debugstr = "";//Logging debug information
var $err = "";//Logging error message

Constructors
$uploadfield the field name of the uploaded file, upload multiple files can be an array
$uploadpath file upload path, cannot be an array
$maxsize limit the size of uploaded files, upload multiple files can be an array, for different files to limit different sizes
$ftype limit the type of upload file, upload multiple files can be an array, for different files to limit different types of
$ftype as ' all ' for unrestricted type
When used, you can set the value of $ftype to Array (' jpg,gif,png ', ' swf ', ' all ') for three upload file restriction types. The first file must be a JPG or GIF or PNG, the second must be a SWF, and the third is any type
function lwgupload ($uploadfield = "", $uploadpath = "", $maxsize = "", $ftype = "All") {
$this->uploadfield= $uploadfield;
$this->uploadpath= $uploadpath;
$this->maxsize= $maxsize;
$this->ftype= $ftype;
}

Test () tests whether to upload all or not one
$oldfile represents the file to be deleted, used in the update, delete the old uploaded new
function Test ($oldfile = ' ") {
if ($this->uploadfield== "") return;
if (Empty ($this->uploadpath)) $this->uploadpath=dirname ($_server[' path_translated ')). " /upload ";
if (Empty ($this->maxsize)) $this->maxsize=1048576;
if (!is_array ($this->uploadfield)) $this->uploadfield=array ($this->uploadfield);
If you do not change to an array, it is easy to simplify the following code
$total _upload=count ($this->uploadfield);//Get total number of fields

if (!is_array ($this->maxsize)) $this->maxsize=array ($this->maxsize);//If it is not an array, it is easy to simplify the following code
if (!is_array ($this->ftype)) $this->ftype=array ($this->ftype);/ibid.

If no corresponding path is established
if (!file_exists ($this->uploadpath)) {
if (!mkdir ($this->uploadpath,0700)) Return output ("Error!)" Please manually establish a valid path. ");
}

Include_once (DirName (__file__). " /lwgfiletype.inc.php ");
The file contains the array variable $filetype, which records the extensions for most file types

Set_time_limit (60);/set timeout of 60 seconds

$needupload =array ()//files that can be uploaded by test

for ($i =0; $i < $total _upload; $i + +) {
$this->uploadfile[$i]=$_files[$this->uploadfield[$i]][' tmp_name ']; Temporary files
$this->file_name[$i]=$_files[$this->uploadfield[$i]][' name ']; Filename
$this->file_type[$i]=$_files[$this->uploadfield[$i]][' type ']; Type
$this->file_lname[$i]=strtolower (substr (STRRCHR ($this->file_name[$i], "."), 1); Name extension
$this->file_size[$i]=$_files[$this->uploadfield[$i]][' size ']; Size
$this->file_old[$i]=$_post[$this->uploadfield[$i]. ' _old ']; Files that need to be deleted
$this->newfile[$i]= $this->uploadpath. " /". $this->file_name[$i];//uploaded address

$maxsize = (!empty ($this->maxsize[$i]) && $this->maxsize[$i]>0)? $this->maxsize[$i]: $this-> MAXSIZE[0];

$maxsize _value= $this->format_maxsize ($maxsize);//To format $maxsize

$thetype = (!empty ($this->ftype[$i]))? $this->ftype[$i]: $this->ftype[0];
$ftype =array ();
$lname =array ();
if ($thetype!= "all") {
$tmp _type=explode (",", $thetype);
For ($n =0 $n <sizeof ($tmp _type); $n + +) {
$tmp _t=trim ($tmp _type[$n]);
if (!empty ($tmp _t)) {
$s _tmp=strtolower ($tmp _t);
$ftype []= $filetype [$s _tmp];
$lname []= $s _tmp;
}
}
}
Analyze parameters that limit the current type

if (($this->file_size[$i]==0) | | ($this->uploadfile[$i]== "")) {
return $this->output ($this->uploadfield[$i]. " An invalid file or 0-byte file was uploaded. ");
}

if ($thetype!= "All" &&!in_array ($this->file_type[$i], $ftype) &&!in_array ($this->file_lname[ $i], $lname)) {
return $this->output ("Sorry!") The upload file format can only be '. $thetype. '. ");
}

if ($this->file_size[$i] > $maxsize) {
return $this->output ("Sorry!") File '. $this->file_name[$i]. "' is greater than ' ". $maxsize _value." ', upload failed. \ nplease reduce the file to ". $maxsize _value." , and then try again. ");
}

if (file_exists ($this->newfile[$i])) {
if ($oldfile = = "" | | ($oldfile!= "" &&!in_array ($this->newfile[$i], $oldfile)) return $this->output ("Sorry!") File '. $this->file_name[$i]. "' Already exist, upload failed. Please change the file name and try again. ");
$oldfile represents the file to be deleted, although the file to be uploaded exists, but it will be deleted, so it can still be uploaded
}

if (In_array ($this->file_name[$i], $needupload)) {
If more than two upload fields upload the same file is not allowed
return $this->output ("Sorry!") File '. $this->file_name[$i]. "' Repeat, upload failed. Please change the file name and try again. ");
}
$needupload []= $this->file_name[$i];
}
Set_time_limit (30);/set timeout of 30 seconds
return true;
}


Test can be uploaded after clearance
function upload () {
if ($this->uploadfield== "") return false;
$total _upload=count ($this->uploadfield);

for ($i =0; $i < $total _upload; $i + +) {
$this->file_size_format[$i]= $this->format_maxsize ($this->file_size[$i]);

if (@move_uploaded_file ($this->uploadfile[$i], $this->newfile[$i]))
$this->debugstr.= "file". $this->file_name[$i]. "' (". $this->file_size_format[$i].") Uploaded successfully. <br> ";
else {
if ($i >0) $this->err_del (($i-1), $this->newfile);
If one of the uploads fails, delete all uploaded files
return $this->output ("Sorry!") File '. $this->file_name[$i]. "' Upload failed. Please try again. ");
}
}
return true;
}

Format File size number
function Format_maxsize ($value) {
if ($value <1024) $maxsize _value = $value. " BYTE ";
ElseIf ($value <1024*1024) $maxsize _value = Number_format (double) ($value/1024), 2. " KB ";
else $maxsize _value = Number_format (double) ($value/(1024*1024)), 2. " MB ";
return $maxsize _value;
}

Delete uploaded files after an error
function Err_del ($i, $thefile) {
while ($i >=0) {
if (@unlink ($thefile [$i])!=false) $this->debugstr.= "file". $thefile [$i]. " Delete Success <br> ";
$i--;
}
}

Logging and outputting error messages
function output ($msg) {
if ($msg!= "") $this->err= $msg;
if ($this->debug) echo "<script language=\" javascript\ type=\ "text/javascript\" >alert (\ "". $msg. " \ "); History.go ( -1) </script>";
return false;
}
}

/*
----------------How to use-----------------------------------------------
$obj =lwgupload ("Image1", "" ", 1024*2," all ");
if ($obj->test ()) $obj->upload ();
-------------------------------------------------------------------------
*/
?>



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.