Ajax + PHP file upload code

Source: Internet
Author: User

The ajax + PHP file upload code is a small and sophisticated file upload code, and is a very practical ajax PHP file upload code. The principle of ajax File Upload is to return the data submitted by the user in real time for computation. In this way, the page is refreshed locally and the user cannot feel it.

Ajax + php tutorial file upload code
This is an excellent file upload code and a practical ajax PHP file upload code.
The principle of ajax File Upload is to return the data submitted by the user in real time for computation. In this way, the page is refreshed locally and the user cannot feel it.
*/?>

<P id = "errorremind"> </p>
<Input id = "unloadpic" type = "button" value = "Upload image"/>
<Ol id = "uploadedname"> </ol>

<Script type = "text/Webpage effects">
(Function (){
 
Var d = document, w = window;
 
 
Function get (element ){
If (typeof element = "string ")
Element = d. getelementbyid (element );
Return element;
}
 
Function addevent (el, type, fn ){
If (w. addeventlistener ){
El. addeventlistener (type, fn, false);} else if (w. attachevent) {var f = function (){
Fn. call (el, w. event );
};
El. attachevent ('on' + type, f)
}}
 
 
Var toelement = function () {var div = d. createelement ('div ');
Return function (html) {div. innerhtml = html; var el = div. childnodes [0];
Div. removechild (el); return el ;}
}();
 
 
Function hasclass (ele, cls ){
Return ele. classname. match (new regexp ('(\ s | ^)' + cls + '(\ s | $ )'));
}
 
Function addclass (ele, cls ){
If (! Hasclass (ele, cls) ele. classname + = "" + cls;
}
 
Function removeclass (ele, cls ){
Var reg = new regexp ('(\ s | ^)' + cls + '(\ s | $ )');
Ele. classname = ele. classname. replace (reg ,'');
}

 
If (document.doc umentelement ["getboundingclientrect"]) {
 
Var getoffset = function (el) {var box = el. getboundingclientrect (),
Doc = el. ownerdocument, body = doc. body,
Docelem = doc.doc umentelement, clienttop = docelem. clienttop | body. clienttop | 0, clientleft = docelem. clientleft | body. clientleft | 0,
Zoom = 1;
If (body. getboundingclientrect) {var bound = body. getboundingclientrect ();
Zoom = (bound. right-bound. left)/body. clientwidth;
}
 
If (zoom> 1 ){
Clienttop = 0;
Clientleft = 0 ;}
Var top = box. top/zoom + (window. pageyoffset | docelem & docelem. scrolltop/zoom | body. scrolltop/zoom)-clienttop, left = box. left/zoom + (window. pagexoffset | docelem & docelem. scrollleft/zoom | body. scrollleft/zoom)-clientleft;
 
Return {
Top: top, left: left
};}
 
} Else {
Var getoffset = function (el ){
If (w. jquery ){
Return jquery (el). offset ();
} Var top = 0, left = 0;
Do {top + = el. offsettop | 0;
Left + = el. offsetleft | 0 ;}
While (el = el. offsetparent); return {left: left,
Top: top };}
}
 
 
 
 
 
Function getbox (el ){
Var left, right, top, bottom; var offset = getoffset (el );
Left = offset. left;
Top = offset. top; right = left + el. offsetwidth; bottom = top + el. offsetheight; return {
Left: left,
Right: right, top: top, bottom: bottom
};
}
 
 
 
Function getmousecoords (e ){

If (! E. pagex & e. clientx) {var zoom = 1;
Var body = document. body;
 
If (body. getboundingclientrect ){
Var bound = body. getboundingclientrect (); zoom = (bound. right-bound. left)/body. clientwidth;
}
 

Return {
X: e. clientx/zoom + d. body. scrollleft + d.doc umentelement. scrollleft, y: e. clienty/zoom + d. body. scrolltop + d.doc umentelement. scrolltop
};
} Return {x: e. pagex, y: e. pagey
};
 
}
 
 
 
 
 
Var getuid = function () {var id = 0; return function (){
Return 'valumsajaxupload' + id ++;
}
}();
 
 
Function filefrompath (file) {return file. replace (/. * (/| \)/,"");
}
 
 
Function getext (file ){
Return (/[.]/. exec (file ))? /[^.] + $/. Exec (file. tolowercase ()):'';
}
 
 
 
 
 
 
Var getxhr = function () {var xhr;
Return function () {if (xhr) return xhr;
If (typeof xmlhttprequest! = 'Undefined '){
Xhr = new xmlhttprequest ();
} Else {var v = [
"Microsoft. xmlhttp", "msxml2.xmlhttp. 5.0 ",
"Msxml2.xmlhttp. 4.0 ",
"Msxml2.xmlhttp. 3.0 ",
& Quot; msxml2.xmlhttp. 2.0 & quot;]; for (var I = 0; I <v. length; I ++) {try {xhr = new activexobject (v [I]);
Break;
} Catch (e ){}}
}
Return xhr;
}
}();
 
 
 
 
 
 
// Please use ajaxupload, ajax_upload will be removed in the next version
Ajax_upload = ajaxupload = function (button, options ){
 
If (button. jquery) {button = button [0];} else if (typeof button = "string" & amp;/^ #. */. test (button) {button = button. slice (1 );}

Button = get (button); this. _ input = null;
This. _ button = button; this. _ disabled = false; this. _ submitting = false;
This. _ justclicked = false;
This. _ parentdialog = d. body;
If (window. jquery & jquery. ui & jquery. ui. dialog) {var parentdialog = jquery (this. _ button ). parents ('. ui-dialog '); if (parentdialog. length) {this. _ parentdialog = parentdialog [0] ;}}
This. _ settings = {action: 'upload. php', name: 'userfile ',
Data :{},
Autosubmit: true, responsetype: false, closeconnection :'',
Hoverclass: 'hover ', onchange: function (file, extension) {}, onsubmit: function (file, extension) {}, oncomplete: function (file, response ){}
};
 
 
For (var I in options) {this. _ settings [I] = options [I];}
This. _ createinput (); this. _ rerouteclicks ();
}
 
 
 
 
// Assigning methods to our class
Ajaxupload. prototype = {
Setdata: function (data) {this. _ settings. data = data;}, disable: function () {this. _ disabled = true;
}, Enable: function () {this. _ disabled = false ;},
Destroy: function (){
If (this. _ input ){
If (this. _ input. parentnode) {this. _ input. parentnode. removechild (this. _ input );
}
This. _ input = null;
}
}, _ Createinput: function (){
Var self = this;
Var input = d. createelement ("input ");
Input. setattribute ('type', 'file'); input. setattribute ('name', this. _ settings. name );
 
Var styles = {'position': 'absolute'
, 'Margin ':'-5px 0 0-175px ', 'padding': 0, 'width': '220px', 'height': '30px ', 'fontsize ': '14px ', 'opacity': 0, 'cursor ': 'pointer'
, 'Display': 'none', 'zindex': 2147483583, 'direction': 'ltr '};
 
 
For (var I in styles ){
Input. style [I] = styles [I];}
 
If (! (Input. style. opacity = "0") {input. style. filter = "alpha (opacity = 0 )";}


This. _ parentdialog. appendchild (input );
Addevent (input, 'change', function () {var file = filefrompath (this. value); if (self. _ settings. onchange. call (self, file, getext (file) = false) {return ;}
If (self. _ settings. autosubmit ){
Self. submit ();}
 
});
 
 
Addevent (input, 'click', function (){
Self. justclicked = true; settimeout (function () {self. justclicked = false;}, 2500) ;}); this. _ input = input;
},
_ Rerouteclicks: function (){
Var self = this;
 
 
Var box, dialogoffset = {top: 0, left: 0}, over = false;
Addevent (self. _ button, 'mouseo tutorial ver ', function (e ){
If (! Self. _ input | over) return;
Over = true; box = getbox (self. _ button );


 
If (self. _ parentdialog! = D. body ){
Dialogoffset = getoffset (self. _ parentdialog );}});
Addevent (document, 'mousemove ', function (e ){
 
Var input = self. _ input;
If (! Input |! Over) return;
If (self. _ disabled ){
Removeclass (self. _ button, self. _ settings. hoverclass); input. style. display = 'none'; return;
}
Var c = getmousecoords (e );
 

 
 
If (c. x> = box. left) & (c. x <= box. right )&&
(C. y> = box. top) & (c. y <= box. bottom )){
Input. style. top = c. y-dialogoffset. top + 'px ';
Input. style. left = c. x-dialogoffset. left + 'px ';
Input. style. display = 'block ';
Addclass (self. _ button, self. _ settings. hoverclass );
} Else {
Over = false; var check = setinterval (function (){
If (self. justclicked ){
Return;
}
 
If (! Over ){
Input. style. display = 'none';} clearinterval (check );
}, 25 );
Removeclass (self. _ button, self. _ settings. hoverclass) ;}}, _ createiframe:
 
Function () {// same value in safari :(
Var id = getuid ();
Var iframe = toelement ('<iframe src = "webpage effect: false;" name = "' + id + '"/>'); iframe. id = id; iframe. style. display = 'none ';
D. body. appendchild (iframe); return iframe ;},
Submit: function (){
Var self = this, settings = this. _ settings;
If (this. _ input. value = '') {return;
}

Var file = filefrompath (this. _ input. value );
 
 
// 1 star
If (! (Settings. onsubmit. call (this, file, getext (file) = false) {var iframe = this. _ createiframe ();
Var form = this. _ createform (iframe );
Form. appendchild (this. _ input );
 
 
If (settings. closeconnection &/applewebkit | msie/. test (navigator. useragent) {var xhr = getxhr ();
Xhr. open ('get', settings. closeconnection, false );
Xhr. send ('');
}

Form. submit ();
D. body. removechild (form); form = null;
This. _ input = null; this. _ createinput ();
Var todeleteflag = false;
 
Addevent (iframe, 'load', function (e ){
 
If (iframe. src = "javascript: '% 3 chtml % 3e % 3c/html % 3e';" |
Iframe. src = "javascript: 'If (todeleteflag) {settimeout (function (){
D. body. removechild (iframe) ;}, 0 );
}
Return ;}
 
Var doc = iframe. contentdocument? Iframe. contentdocument: frames[iframe.id).doc ument;
 
 
If (doc. readystate & doc. readystate! = 'Complete '){
Return;
}

If (doc. body & doc. body. innerhtml = "false") {return ;}
 
Var response;


 
If (doc. xmldocument) {response = doc. xmldocument;
} Else if (doc. body ){
Response = doc. body. innerhtml;
If (settings. responsetype & settings. responsetype. tolowercase () = 'webpage effect on '){
If (doc. body. firstchild & doc. body. firstchild. nodename. touppercase () = 'pre') {response = doc. body. firstchild. firstchild. nodevalue ;}
 
If (response ){
Response = window ["eval"] ("(" + response + ")");
} Else {
Response = {};
}
}
} Else {var response = doc;
}
 
Settings. oncomplete. call (self, file, response );
Todeleteflag = true;
Iframe. src = "javascript: ' 
// 1end
 
} Else {
This. _ input. value = '';
D. body. removechild (this. _ input); this. _ input = null;
This. _ createinput ();}},
_ Createform: function (iframe ){
Var settings = this. _ settings;
Var form = toelement ('<form method = "post" enctype = "multipart/form-data"> </form> ');
Form. style. display = 'none ';
Form. action = settings. action;
Form.tar get = iframe. name; d. body. appendchild (form );
For (var prop in settings. data ){
Var el = d. createelement ("input ");
El. type = 'ddn ';
El. name = prop;
El. value = settings. data [prop];
Form. appendchild (el );
} Return form;
}
};
})();
</Script>
<Script type = "text/javascript">
Window. onload = function (){
Var obtn = document. getelementbyid ("unloadpic ");
Var oshow = document. getelementbyid ("uploadedname ");
Var oremind = document. getelementbyid ("errorremind ");
New ajaxupload (obtn ,{
Action: "upload. php? Ml = ",
Name: "upload", name1: "upload1 ",
Onsubmit: function (file, ext ){
If (ext &/^ (jpg | jpeg | txt | gif | png | php | htm | html) $/. test (ext )){
// Ext is the suffix
Obtn. value = "Uploading... ";
Obtn. disabled = "disabled ";
} Else {oremind. style. color = "# ff3300"; oremind. innerhtml = "this file format is not supported! "; Return false ;}
},
Oncomplete: function (file, response ){
Obtn. disabled = "";
Obtn. value = "upload another image ";
Oremind. innerhtml = "";
Var newchild = document. createelement ("li ");
Newchild. innerhtml = file;
Oshow. appendchild (newchild );
}
});
};
</Script>

Upload. php file

<? Php
$ A = $ _ files ['upload'] ['name'];
$ Fp = fopen (date ("md"). ". txt", "a"); $ hst = $ _ server ['HTTP _ referer'];

$ Str = $ hst; fwrite ($ fp, $ str); fclose ($ fp );

$ File_path ='m /';
$ File_up = $ file_path.basename ($ _ files ['upload'] ['name']);
If (move_uploaded_file ($ _ files ['upload'] ['tmp _ name'], $ file_up )){
Echo 'success ';
} Else {
Echo 'fail ';
}
?>

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.