Php ajax example code for refreshing upload of images

Source: Internet
Author: User

AJAX client Page code: index.html
Copy codeThe Code is as follows:
<Html>
<Body>
<H1> Ajax file upload sample <Div id = result> </div>
<PRE class = js name = "code"> <script language = JavaScript>
// Upload function
Function btn_send.onclick (){
Data = ""
Spliter = "------- 7d8d733180846"
Datadata = data + spliter + "\ r \ n"
Datadata = data + "Content-Disposition: form-data; name = \" photofile \ "; filename = \" C: \ a.txt \ "\ r \ n"
// Datadata = data + "Content-Type: image/pjpeg" + vbCrLf
Datadata = data + "Content-Type: text/plain" + "\ r \ n" + "\ r \ n"
Text = "My name is Wilson Lin ."
PostLength = text. length + data. length + 2 + spliter. length + 4
Package = data + text + "\ r \ n" + spliter + "-- \ r \ n"

Alert (package)
// Send the XML document to the Web server
Var xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
Xmlhttp. open ("POST", "./upload. php", false );
Xmlhttp. setRequestHeader ("Content-Type", "multipart/form-data; boundary = ----- 7d8d733180846 ");
Xmlhttp. setRequestHeader ("Content-Length", postLength );
Xmlhttp. send (package );
// Display the information returned by the server
Result. innerHTML = xmlhttp. ResponseText;
}
</SCRIPT>
</PRE>
</Body>
</Html>

PHP server code: upload. php
Copy codeThe Code is as follows:
<? Php
// $ _ FILES ['photofile']: An array for retrieving uploaded images
// $ Uploadfile: storage address
$ Uploadfile = "D:/". $ _ FILES ['photofile'] ['name'];
Copy ($ _ FILES ['photofile'] ['tmp _ name'], $ uploadfile );
Echo "URL: <a href = 'HTTP: // localhost /". $ _ FILES ['photofile'] ['name']. "'target = '_ blank'> ". $ _ FILES ['photofile'] ['name']. "</a> <br/> ";
?>
Upload successed!

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.