Php + jquery Ajax asynchronous Image Upload (ajaxSubmit) instance

Source: Internet
Author: User

Let's take a look at a php + jquery Ajax asynchronous Image Upload (ajaxSubmit) instance. We actually use ajax instead of iframe.

The effect is as follows:

The Code is as follows: Copy code

Index. php file

Php uses jquery to upload images asynchronously (ajaxSubmit). The Code for submitting the page is as follows:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Ajax uploads images asynchronously. </title>
<Script src = "js/jquery-1.4.2.js" language = "JavaScript" type = "text/javascript"> </script>
<Script src = "js/jquery. form. js" language = "JavaScript" type = "text/javascript"> </script>
<Script type = "text/javascript">

Function sky_upfiles (){
Var messtxt;
$ ("# Sky_upform"). ajaxSubmit ({
// DataType: 'script ',
Type: 'post ',
Url: "doupfiles. php ",
BeforeSubmit: function (){
$ ("# Sky_txt" ).html ("uploading images ...");
},
Success: function (data ){
If (data = "1 "){
Messtxt = "Upload successful! ";
} Else if (data = "-1 "){
Messtxt = "the file size exceeds the specified size! ";
} Else if (data = "-2 "){
Messtxt = "the file type does not match! ";
} Else if (data = "-3 "){
Messtxt = "An error occurred while moving the file! ";
} Else {
Messtxt = "Unknown error! ";
}
$ ("# Sky_txt" pai.html (messtxt );
// $ ("# Sky_txt"). append (data );
},
ResetForm: false,
ClearForm: false
});
// $ ("# Upimgform"). submit ();
}

</Script>
</Head>
<Body>
<Br/>
<Fieldset style = "width: 97%">
<Legend> upload a file </legend>
<Form enctype = "multipart/form-data" id = "sky_upform" name = "sky_upform" action = "" method = "post">
<Table width = "100%" border = "0" align = "center" cellpadding = "3" cellspacing = "0">
<Tr>
<Td width = "100" align = "right" class = "F-12"> </td>
<Td class = "F-12" align = "left">
<Input name = "upfile" id = "upfile" maxlength = "20" size = "40" type = "file" value = ""/> <input name = "upmit" type = "button" id = "upmit" value = "Upload" onclick = "sky_upfiles () "/>
<Font color = "red" id = "sky_txt"> </font> </td>
</Tr>
</Form>

</Body>
</Html>

Doupfiles. php file

The Code is as follows: Copy code

<? Php
/*
* 1: Upload successful
*-1: The file size exceeds the specified size.
*-2: The file type does not match
*-3: An error occurred while moving the file.
*/
If (is_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name']) {

$ Photo_types = array ('image/jpg ', 'image/jpeg', 'image/png ', 'image/pjpeg', 'image/gif ', 'image/bmp ', 'image/x-png'); // defines the upload format
$ Max_size = 700000; // maximum size of the uploaded photo. The default value is 700 KB.
$ Photo_folder = "upload /". date ("Y "). "/". date ("m "). "/". date ("d "). "/"; // upload the photo path
//////////////////////////////////////// /// // Start processing the upload
If (! File_exists ($ photo_folder) // check whether the photo directory exists
{
Mkdir ($ photo_folder, 0777, true); // mkdir ("temp/sub, 0777, true );
}

$ Upfile = $ _ FILES ['upfile'];
$ Name = $ upfile ['name'];
$ Type = $ upfile ['type'];
$ Size = $ upfile ['SIZE'];
$ Tmp_name = $ upfile ['tmp _ name'];

$ File = $ _ FILES ["upfile"];
$ Photo_name = $ file ["tmp_name"];
// Echo $ photo_name;
$ Photo_size = getimagesize ($ photo_name );

If ($ max_size <$ file ["size"]) // check the file size
Echo "-1"; // echo "<script> alert ('Sorry, the file exceeds the specified size! '); History. go (-1); </script> ";
If (! In_array ($ file ["type"], $ photo_types) // check the file type
Echo "-2"; // echo "<script> alert ('Sorry, the file type does not match! '); History. go (-1); </script> ";
If (! File_exists ($ photo_folder) // photo directory
Mkdir ($ photo_folder );
$ Pinfo = pathinfo ($ file ["name"]);
$ Photo_type = $ pinfo ['extension']; // Upload File extension
$ Photo_server_folder = $ photo_folder.time (). ".". $ photo_type; // use the current time and a 7-bit random number as the file name. Here is the complete upload path.


If (! Move_uploaded_file ($ photo_name, $ photo_server_folder ))
{
Echo "-3"; // echo "An error occurred while moving the file ";
Exit;
}
$ Pinfo = pathinfo ($ photo_server_folder );
$ Fname = $ pinfo ['basename'];
Echo "1"; // echo "uploaded successfully:". $ photo_server_folder. "<br/> ";

 


}
?>

Finally, complete php + jquery Ajax asynchronous upload image (ajaxSubmit) instance source code

 

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.