Ajax file Upload

Source: Internet
Author: User
Tags file upload php tutorial
The code is as follows: Copy code

<! 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 = gb2312"/>
<Title> ajax file Upload </title>
<Style type = "text/css tutorial">
# F1_upload_process {display: none ;}
</Style>
<Script language = "webpage effect" type = "text/javascript">
<! --

Function $ (id)
{
Return document. getelementbyid (id );
}

Function startupload (){
If ($ ('myfile'). value = '')
   {
Alert ('select the image to upload! ');
Return false;
   }
Var array = $ ('myfile'). value. split ('.');
Var ext = array [1]. tolowercase ();
If (ext = "gif" | ext = "jpg" | ext = "png ")
   {   
$('F1 _ upload_process '). style. display = 'block ';
$('F1 _ upload_form '). style. display = 'block ';
Return true;
   }
Else
   {
Alert ('only images in gif. jpg png format can be uploaded! ');
Return false;
   }
}

Function stopupload (success, pic ){
Var result = '';
If (success = 1 ){
Result = ' ';
$ ('Logo '). value = pic;
      }
Else {
Result = '<span class = "emsg"> failed to upload logo images. Please contact the developer! </Span> <br/> ';
      }
$('F1 _ upload_process '). style. display = 'none ';
$('F1 _ upload_form '). innerhtml = result + '<br/> <label> <input name = "myfile" type = "file" size = "30"/> </label> <input type = "submit" name = "submitbtn" class = "sbtn" value = "upload image"/> </label> ';
$('F1 _ upload_form '). style. display = 'block ';
Return true;
}

// -->
</Script>
</Head>

<Body>
<Form action = "upload. php Tutorial "method =" post "enctype =" multipart/form-data "target =" upload_target "onsubmit =" javascript: return startupload (); ">
<Span id = "f1_upload_process"> </span>
<Span id = "f1_upload_form" align = "center">
<Input name = "myfile" type = "file" id = "myfile" size = "30"/>
<Input type = "submit" name = "submitbtn" class = "sbtn" value = "upload image"/>
       
</Span>
    
<Iframe id = "upload_target" name = "upload_target" src = "#" style = "width: 0; height: 0; border: 0px solid # fff; "> </iframe>
(You can upload gif, jpg, and png files)
</Form>
</Body>
</Html>

Upload. php

The code is as follows: Copy code

<? Php
$ Destination_path = '.../upfile/jianjulogo/'; // getcwd (). directory_separator;

$ Result = 0;
  
$ Target_path = $ destination_path. basename ($ _ files ['myfile'] ['name']);

If (@ move_uploaded_file ($ _ files ['myfile'] ['tmp _ name'], $ target_path )){
$ Result = 1;
   }
Echo $ target_path;
Sleep (1 );


?>
<Script language = "javascript" type = "text/javascript"> window. top. window. stopupload (<? Php echo $ result;?>, '<? = $ Target_path?> '); </Script>

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