/*
Bytes ------------------------------------------------------------------------------------
Class name: Lwguploadandquery
Description: SQL statement execution and file upload are subclasses of the Lwgupload class.
Author: long Weiguo
Network user: lwg888
Email: lwg888@163.com
Use, modify, and disseminate author information
Bytes ------------------------------------------------------------------------------------
*/
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 which the object is redirected after the file is uploaded successfully
// Important: SQL statement syntax:
// $ 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 domain, followed by the suffix "_ name", so that you can locate 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 are no executable SQL statements. ");
If (! Is_array ($ this-> uploadfield) $ this-> uploadfield = array ($ this-> uploadfield); // If the array is not changed to an array, the subsequent code is simplified
If (! Is_array ($ this-> maxsize) $ this-> maxsize = array ($ this-> maxsize); // same as above
If (! Is_array ($ this-> ftype) $ this-> ftype = array ($ this-> ftype); // same as above
$ 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']; // used in an update to indicate the original file corresponding to the uploaded file
$ File_del = $ _ POST [$ this-> uploadfield [$ I]. '_ del']; // whether the original file is marked as deleted
If ($ uploadfile! = "" & $ File_name! = ""){
// Indicates the file to be uploaded, that is, the file field is filled with any characters
$ Uploadfield [] = $ this-> uploadfield [$ I];
// Obtain a new Upload field because the file field is not filled with any characters and does not need to be uploaded
$ Maxsize [] = (! Empty ($ this-> maxsize [$ I])? $ This-> maxsize [$ I]: $ this-> maxsize [0];
// Obtain the corresponding size limit value
$ Ftype [] = (! Empty ($ this-> ftype [$ I])? $ This-> ftype [$ I]: $ this-> ftype [0];
// Obtain the corresponding type limit value
$ Newfile = $ this-> uploadpath. "/". $ file_name;
// Address of the uploaded file
$ Insertname = $ file_name;
// Used to modify the SQL language. change the value of the corresponding field in the mysql table to the current uploaded file name.
If ($ file_old! = "") $ Oldfile [] = $ this-> uploadpath. "/". sprintf ('% s', $ file_old );
// Used in updates to obtain the original file address. when a new file is uploaded, the old file is deleted.
}
Else if ($ file_old! = ""){
// Indicates that no upload is required, that is, the file domain is not filled with any characters, but there are old files
If ($ file_del = "true "){
// If the old file is marked as deleted
$ Insertname = "";
// Used to modify the SQL language. change the value of the corresponding field in the mysql table to null.
$ Oldfile [] = $ this-> uploadpath. "/". sprintf ('% s', $ file_old );
}
Else $ insertname = sprintf ('% s', $ file_old );
// You do not need to upload or delete the data. you need to modify the SQL language and change the value of the corresponding field in the mysql table to the original file name.
}
Else $ insertname = "";
// You do not need to upload or use the original file. you need to modify the SQL language and change the value of the corresponding field in the mysql table to null.
$ This-> SQL = str_replace ($ this-> uploadfield [$ I]. "_ name", $ insertname, $ this-> SQL );
// Modify the SQL statement
}
/*
Bytes ------------------------------------------------------------------------------------
Class name: Lwgupload
Description: multifile Upload
Author: long Weiguo
Network user: lwg888
Email: lwg888@163.com
Use, modify, and disseminate author information
Bytes ------------------------------------------------------------------------------------
*/
Class Lwgupload {
// ------------- You can set the value variable ------------------------
Var $ uploadfield; // field name of the uploaded file
Var $ maxsize; // restrict the size of the uploaded file
Var $ file_old; // the old file to be deleted
Var $ uploadpath; // file Upload path
Var $ ftype; // restrict the types of uploaded files
Var $ debug = true; // whether to display debugging or error messages
// ------------ The variable used to obtain the value ---------------------------
Var $ uploadfile; // temporary file after Upload
Var $ file_name; // file name
Var $ file_size; // file size
Var $ file_size_format; // formatted $ file_size
Var $ file_type; // file type
Var $ debugstr = ""; // record debugging information
Var $ err = ""; // record error information
// Constructor
// $ Uploadfield specifies the field name of the file to be uploaded. it can be an array when multiple files are uploaded.
// $ Upload path of the uploadpath file, which cannot be an array
// $ Maxsize limits the size of the uploaded files. when uploading multiple files, it can be an array, which limits the size of different files.
// $ Ftype indicates the type of the file to be uploaded. when uploading multiple files, it can be an array. different types of files are restricted.
// When the $ ftype is 'all', the type is not limited.
// You can set the value of $ ftype to array ('jpg, gif, png ', 'swf', 'all') to three Upload file restriction types. The first file must be jpg, gif, or png, the second file must be swf, and the third file must be of any type.
Function Lwgupload ($ uploadfield = "", $ uploadpath = "", $ maxsize = "", $ ftype = "all "){
$ This-> uploadfield = $ uploadfield;
$ This-> uploadpath = $ uploadpath;
$ This-> maxsize = $ maxsize;
$ This-> ftype = $ ftype;
}
// Test () is used to test whether all objects can be uploaded. otherwise, do not upload one.
// $ Oldfile indicates the file to be deleted. it is used in updates to delete the old file
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 the array is not changed to an array, the subsequent code can be simplified.
$ Total_upload = count ($ this-> uploadfield); // obtain the total number of fields
If (! Is_array ($ this-> maxsize) $ this-> maxsize = array ($ this-> maxsize); // If the array is not changed to an array, the subsequent code is simplified
If (! Is_array ($ this-> ftype) $ this-> ftype = array ($ this-> ftype); // same as above
// If no corresponding path exists, it is created
If (! File_exists ($ this-> uploadpath )){
If (! Mkdir ($ this-> uploadpath, 0700) return output ("error! Create a valid path manually. ");
}
Include_once (dirname (_ FILE _). "/Lwgfiletype. inc. php ");
// This file contains the array variable $ filetype, which records the extension of most File types
Set_time_limit (60); // Set the timeout value to 60 seconds.
$ Needupload = array (); // files that can be uploaded after testing
If ($ thetype! = "All "&&! In_array ($ this-> file_type [$ I], $ ftype )&&! In_array ($ this-> file_lname [$ I], $ lname )){
Return $ this-> output ("Sorry! The format of the uploaded file can only be '". $ thetype ."'. ");
}
If ($ this-> file_size [$ I]> $ maxsize ){
Return $ this-> output ("Sorry! File '". $ this-> file_name [$ I]."' Greater than '". $ maxsize_Value."', upload failed. \ N please 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! The file '". $ this-> file_name [$ I]."' already exists. Upload failed. Change the file name and try again. ");
// $ Oldfile indicates the object to be deleted. Although the object to be uploaded exists, 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
Return $ this-> output ("Sorry! The file '". $ this-> file_name [$ I]."' is repeated and the upload fails. Change the file name and try again. ");
}
$ Needupload [] = $ this-> file_name [$ I];
}
Set_time_limit (30); // Set the timeout value to 30 seconds.
Return true;
}
// Upload after passing the test
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]. ") the upload is successful.
";
Else {
If ($ I> 0) $ this-> err_del ($ i-1), $ this-> newfile );
// If one Upload fails, all uploaded files will be deleted.
Return $ this-> output ("Sorry! File '". $ this-> file_name [$ I]."' Upload failed. Please try again. ");
}
}
Return true;
}
// Format the file size and 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. = "'". $ thefile [$ I]. "' deleted successfully.
";
$ I --;
}
}
// Record and output error messages
Function output ($ msg ){
If ($ msg! = "") $ This-> err = $ msg;
If ($ this-> debug) echo"
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.