How to use the PHP encapsulation class to upload pictures can be directly referenced

Source: Internet
Author: User

 <?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 '] &lt;= $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; }?&gt;//-------------------------------------------------------------------------------------&lt;?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-&gt;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 ... &lt;br/&gt;&lt;br/&gt; "; } else{echo "Failed to add ..."
                 "; }?&gt;//--------------------------------------------------------------------------------------&lt;!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" &gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "&gt; &lt;title&gt; photo Upload class &lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form enctype=" multipart/ Form-data "method=" post "action=" images.php "&gt; Name: &lt;input type=" text "name=" user "id=" user "/&gt;&lt;br&gt; Picture: &lt;input type= "file" Name= "img" id= "img"/&gt;&lt;br&gt; &lt;input type= "Submit" value= "submitted"/&gt; &lt;/form&gt; &lt;/ Body&gt; &lt;/html&gt;

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.