Copy Code code as follows:
<span style= "Font-family:verdana, ' Ms Song ', Arial, arial, Microsoft Ya-hei, Helvetica, Sans-serif; font-size:14px; line-height:21px; Text-align:left; Background-color:rgb (255, 254, 247); ">avascript part </span>
Copy Code code as follows:
<!doctype html>
<meta Cha Rset=utf-8>
<title>ajax file upload ~~</title>
<script>
Window.onload=function () {
var form=document.getelementsbytagname (' form ') [0];
Form.onsubmit=function () {
var iframe=document.createelement (' iframe ');
iframe.src= ' do_upload.php ';
var iframe_name= "iframe" +math.random ();
Iframe.name=iframe_name;
Document.body.appendChild (IFRAME);
iframe.style.width= ' 0px ';
iframe.style.height= ' 0px ';
iframe.frameborder= ' 0 ';
Form.target=iframe_name;
}
</script> <body>
<form enctype= ' multipart/form-data ' method= ' post ' action= ' do_upload.php ' >
Please select file: <input type= ' file ' name= ' myFile '/><br/>
<input type= ' Submit '/> R><div id= ' msg ' ></div>
</form>
≪/body>
//php Receive part
Copy Code code as follows:
<?php
$up _file=$_files[' MyFile '];
if ($up _file[' ERROR ']===0) {
if (!file_exists ('./imgs ')) {
mkdir ('./imgs ');
}
$save _name=rand (). $up _file[' name ';
$bool =move_uploaded_file ($up _file[' tmp_name '), "./imgs/$save _name");
if ($bool) {
$msg = ' upload success! ';
}else{
$msg = ' upload failed! ';
}
}
Echo
"<script>
var Msg=parent.document.getelementbyid (' msg ');
Msg.innerhtml= ' <font color=red> $msg </forn> ';
</script> ";
?>