<?php
Class Image {/** * complete image upload * * @param array $file The file information to be uploaded, the array for 5 elements * @return Mixed if executed successfully, return Returns the file name that was uploaded, otherwise return false */Public function upload ($file) {if ($file [' error '] = = 0) {$allow _types
= Array (' Image/jpeg ', ' image/pjpeg ', ' image/png ', ' image/gif ');
if (In_array ($file [' type '], $allow _types)) {$maxsize = 2000000;
if ($file [' Size '] <= $maxsize) {//upload///need to rename the file, 1, prevent irregular characters from appearing in filename, 2, prevent duplicate names How do I get the suffix name with time stamp plus random number?
Get the suffix name in the original filename//The last point in the filename is intercepted to the end is the extension//STRRCHR (in which string, check the string); $new _filename = time (). Mt_rand (10000, 99999).
STRRCHR ($file [' name '], '. '); Move//This function returns whether the move succeeds or fails if (Move_uploaded_file ($file [' tmp_name '], ' images/'. $new _filen
AME)) {return $new _filename; Only one case returns the file name, and all other returns false return false; }?>//-------------------------------------------------------------------------------------<?php Header ("
Content-type:text/html;charset=utf-8 "); function __autoload ($image) {require_once ($image. ').
Class.php ');
} $image = new image ();
$user = $_post[' user '];
$img = $_files[' img '];
Var_dump ($IMG);
$img = $image->upload ($img);
mysql_connect (' localhost ', ' root ', ' 123 ');
mysql_select_db (' Lyb ');
mysql_query (' Set names UTF8 ');
$q = "Insert Test_image (Name,url) VALUES (' $user ', ' $img ')";
Var_dump ($q);
$result = mysql_query ($q);
if ($result) {echo added successfully ... <br/><br/> "; } else{echo "Failed to add ..."
"; }?>//--------------------------------------------------------------------------------------<!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "> <title> photo Upload class </title> </head> <body> <form enctype=" multipart/ Form-data "method=" post "action=" images.php "> Name: <input type=" text "name=" user "id=" user "/><br> Picture: <input type= "file" Name= "img" id= "img"/><br> <input type= "Submit" value= "submitted"/> </form> </ Body> </html>