Php+jquery+ajax to achieve multiple image upload effect _jquery

Source: Internet
Author: User
Tags extend rand jquery library

Today, I share with you the premise of not refreshing the page, using Php+jquery+ajax to achieve multiple image upload effect. Users simply click to select the image to upload, and the picture is automatically uploaded to the server and displayed on the page.


We'll put a form form on the page, use Post to submit to the background PHP handler upload.php, and note that the Enctype property setting supports file uploads. #preview用来显示上传完毕后的图片. About CSS style settings This article does not add, please refer to the download package source.

Copy Code code as follows:

<form id= "Imageform" method= "post" enctype= "Multipart/form-data" action= "upload.php" >
<div id= "Up_status" style= "Display:none" ></div>
<div id= "up_btn" class= "BTN" >
<span> Add Pictures </span>
<input id= "photoimg" type= "file" name= "Photoimg" >
<p> Max 100KB, support jpg,gif,png format. </p>
<div id= "Preview" ></div>

This example is based on jquery, so you must load the jquery library and Jquery.wallform.js in the page.

Copy Code code as follows:

<script type= "Text/javascript" src= "Jquery.min.js" ></script>
<script type= "Text/javascript" src= "Jquery.wallform.js" ></script>

When you click the button "Add Picture", the Pop-up Selection file dialog box, select the picture to upload, trigger the Change event. The form #imageform then invokes the Jquery.wallform.js Ajaxform () method, submits the form data to the background PHP processing, and processes the display of the page elements based on the returned results. If the upload is successful, the picture will be displayed on the page in a single arrangement. About the use of ajaxform () can refer to this site article: Ajax form submission Plugin jqery form.

Copy Code code as follows:

$ (function () {
$ (' #photoimg '). Die (' click '). Live (' Change ', function () {
var status = $ ("#up_status");
var btn = $ ("#up_btn");
$ ("#imageform"). Ajaxform ({
Target: ' #preview ',
Beforesubmit:function () { ();
Btn.hide ();
Success:function () {
Status.hide (); ();
Error:function () {
Status.hide (); ();
). submit ();


upload.php processing picture upload, and will upload a good picture in the uploads/directory, note that the directory to have write permission. First you need to detect whether the post is submitted, and then determine whether the picture format, picture size meets the requirements, and then use Move_uploaded_file () to upload the picture, and rename the picture, in the form: Time (). Rand (100,999).

Copy Code code as follows:

$path = "uploads/";
$EXTARR = Array ("JPG", "png", "GIF");
if (Isset ($_post) and $_server[' request_method '] = = "POST") {
$name = $_files[' photoimg ' [' name '];
$size = $_files[' photoimg '] [' size '];
if (empty ($name)) {
Echo ' Please select the picture to upload ';
$ext = Extend ($name);
if (!in_array ($ext, $EXTARR)) {
echo ' Picture format Error! ';
if ($size > (100*1024)) {
echo ' picture size cannot exceed 100KB ';
$image _name = Time (). Rand (100,999). ".". $ext;
$tmp = $_files[' photoimg '] [' tmp_name '];
if (Move_uploaded_file ($tmp, $path. $image _name)) {
Echo ' ;
Echo ' On the wrong! ';
Get file type suffix
function extend ($file _name) {
$extend = PathInfo ($file _name);
$extend = Strtolower ($extend ["extension"]);
return $extend;

Of course, the actual application, can be combined with the database and User Center, the user uploaded pictures stored in the data table, the specific application can be studied by themselves.

The above is the article to share all the content, I hope you can enjoy.

Related Article

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: 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.