I wanted to use a plug-in to upload images. Later I wrote a simple js file to Implement Asynchronous Image Upload. Not to mention that the code is very simple.
Upload. php
If (isset ($ _ FILES ["myfile"])
{
$ Ret = array ();
$ UploadDir = 'images'. DIRECTORY_SEPARATOR.date ("Ymd"). DIRECTORY_SEPARATOR;
$ Dir = dirname (_ FILE _). DIRECTORY_SEPARATOR. $ uploadDir;
File_exists ($ dir) | (mkdir ($ dir, 0777, true) & chmod ($ dir, 0777 ));
If (! Is_array ($ _ FILES ["myfile"] ["name"]) // single file
{
$ FileName = time (). uniqid (). '.'. pathinfo ($ _ FILES ["myfile"] ["name"]) ['extension'];
Move_uploaded_file ($ _ FILES ["myfile"] ["tmp_name"], $ dir. $ fileName );
$ Ret ['file'] = DIRECTORY_SEPARATOR. $ uploadDir. $ fileName;
}
Echo json_encode ($ ret );
}
?>
Index.html
Upload files using Html5 Ajax
<Script type = "text/javascript">
Var xhr;
Function createXMLHttpRequest ()
{
If (window. ActiveXObject)
{
Xhr = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest)
{
Xhr = new XMLHttpRequest ();
}
}
Function UpladFile ()
{
Var fileObj = document. getElementById ("file"). files [0];
Var FileController = 'upload. php ';
Var form = new FormData ();
Form. append ("myfile", fileObj );
CreateXMLHttpRequest ();
Xhr. onreadystatechange = handleStateChange;
Xhr. open ("post", FileController, true );
Xhr. send (form );
}
Function handleStateChange ()
{
If (xhr. readyState = 4)
{
If (xhr. status = 200 | xhr. status = 0)
{
Var result = xhr. responseText;
Var json = eval ("(" + result + ")");
Alert ('image link: \ n' + json. file );
}
}
}
</Script>
Image
Browse...
Upload