Upload to a PHP Server, using Ajax ( XMLHttp POST)

Posted by Krishnanunni on Stack Overflow See other posts from Stack Overflow or by Krishnanunni
Published on 2011-11-28T09:43:46Z Indexed on 2011/11/28 9:51 UTC
Read the original article Hit count: 189

Filed under:
|
|
|

Right now i'm using the below method to Upload a file to PHP

<form enctype="multipart/form-data" action="http://sserver.com/fileupload.php" method="POST">

<input type="hidden" name="MAX_FILE_SIZE" value="30000000" />
<input type="hidden" name="filename" value="file_uploaded.gif" />
<input type="hidden" name="username" value="foobar"/>
 Please choose a file:
 <input name="uploaded" type="file" /><br />
 <input type="submit" value="Upload" />
 </form> 

I read the $_POST and $_FILE in php to complete upload like this.

$target = $_SERVER['DOCUMENT_ROOT']."/test/upload/"; 
 $target = $target . basename( $_FILES['uploaded']['name']) ; 
 echo $target;
 $ok=1; 

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
 {
 echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
 } 
 else {
 echo "Sorry, there was a problem uploading your file.";
 }

My questions is , can i change the above said code (HTML) to an Ajax XMLHttpRequest without changes in PHP.

© Stack Overflow or respective owner

Related posts about php

Related posts about AJAX