Send an AJAX request to implement an upload image displayed on a Web page

Source: Internet
Author: User

1<?PHP2 //1, through the Super global variable to get files[upload image name]3$file = $_files["FILES"]4 //2, to get the format of the picture by STRRCHR5$ext = STRRCHR ($file [' name '], '. ');6 //3, randomly get file name by Uniqid function to avoid duplicate name overwrite7$filename =uniqid (). $exe;8 //4, you can get the name of the picture to exist in the session, in order to avoid later use, this step can write not write;9 session_start ();Ten$_session[' url '] =$str; 19:36:16 One //5, through the Move_uploaded_file function to upload the image of the picture exists in a folder A$bool = Move_uploaded_file ($file [' tmp_name '], '. /.. /static/uploads/'. $filename); - //6. Return the processed picture path to the front end -     if($bool) { theecho ". /static/uploads/". $fileName; -}Else { -echo "Upload failed";  -     }; +?> -  +<! DOCTYPE html> A at  - -<meta charset= "UTF-8" > -<meta name= "viewport" content= "Width=device-width, initial-scale=1.0" > -<meta http-equiv= "x-ua-compatible" content= "Ie=edge" > -<title>Document</title> in<script src= ". /static/assets/vendors/jquery/jquery.min.js "></script> -<script> to$(function () { +$ ("#uploadImg"). On ("Change",function () { -                 //Console.log (this.files); the                 varFile = This. files[0]; *                 vardata =NewFormData (); $Data.append (' file ', file);Panax Notoginseng                 //console.log (data); - $.ajax ({ theType: "POST", +URL: "./api/_addposts.php", A Data:data, theDataType: "JSON", +Successfunction(res) { - Console.log (RES) $                     } $                 }); -             }); -         }) the</script> -Wuyi  the<body> -<input type= "File" Name= "id=" uploadimg "> Wu</body> -  About

Send Ajax request to implement upload picture displayed on Web page

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.