Copy codeThe Code is as follows:
<Span style = "font-family: verdana, 'Ms song',, Arial,, Helvetica, sans-serif; font-size: 14px; line-height: 21px; text-align: left; background-color: rgb (255,254,247); "> avascript section </span>
Copy codeThe Code is as follows:
<! Doctype html>
<Html>
<Head>
<Meta charset = UTF-8>
<Head>
<Title> ajax File Upload ~~ </Title>
</Head>
<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.tar get = iframe_name;
}
}
</Script>
<Body>
<Form enctype = 'multipart/form-data' method = 'post' action = 'do _ upload. php'>
Select a file: <input type = 'file' name = 'myfile'/> <br/>
<Input type = 'submit '/>
<Div id = 'msg '> </div>
</Form>
</Body>
</Html>
// Receiving part of php
Copy codeThe Code is 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 successful! ';
} Else {
$ Msg = 'upload failed! ';
}
}
Echo
"<Script>
Var msgw.parent.doc ument. getElementById ('msg ');
Msg. innerHTML = '<font color = red> $ msg </forn> ';
</Script> ";
?>