Image uploads implemented by php encapsulation class can be directly referenced

Source: Internet
Author: User
& Lt ;? Phpclassimage {/*** complete image Upload ** @ paramarray $ array of file information to be uploaded, used for the array of five elements * @ returnmixed if execution is successful, the php encapsulation class implements direct reference for image uploads, and returns the uploaded file name; otherwise, false is returned. Class image {

/**
* Complete image Upload
*
* @ Param array $ array of file information to be uploaded, used for the array of five elements
* @ Return mixed if the execution is successful, the uploaded file name is returned; otherwise, false is returned.
*/
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
// Rename the file, 1 to prevent irregular characters from appearing in the file name, 2 to prevent duplicate names
// Timestamp and random number form
// How can I obtain the suffix? Get the suffix in the original file name
// Truncate the last vertex in the file name to the end with the extension
// Strrchr (the string to be queried );
$ New_filename = time (). mt_rand (10000,999 99). strrchr ($ file ['name'], '.');

// Move
// This function returns whether the mobile operation is successful or fails.
If (move_uploaded_file ($ file ['tmp _ name'], 'Images/'. $ new_filename )){
Return $ new_filename;
}
}
}
}

// Only one file name is returned, and all others return false.
Return false;
}
}
?>

// Configure //-------------------------------------------------------------------------------------
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 .....

";
}
Else {
Echo "failed to add... ";
}
?>

// Configure //--------------------------------------------------------------------------------------





Image Upload class




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.