By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

PHP Ajax file upload script problem

P: 1
I am trying to upload file using ajax and php but having some problems. it always says that there was a problem and file is not uploaded. here is the code

form and ajax code

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Untitled Document</title>
  6. <script language="javascript">
  7. var xmlHttp
  8.  
  9. function uploadfile()
  10. //;
  11. //var i = 0;
  12.  
  13. // This little bit loops through all the elements in the form, locates the
  14.   // file input field, and extracts the file name of the file being uploaded.
  15.        var fileName = document.form1.uploadedfile.value;
  16.         if (fileName.indexOf('/') > -1) { 
  17.           fileName = fileName.substring(fileName.lastIndexOf('/')+1, fileName.length);
  18.       }    else {
  19.           fileName = fileName.substring(fileName.lastIndexOf('\\')+1, fileName.length);
  20.       }
  21.       //Since there can only be one file per form for this script, we'll exit the loop here. 
  22.  
  23.  
  24. //var str = ;
  25. alert(fileName);
  26. //form1.submit();
  27. //var str = document.form1.tosingleemail.value;
  28. //var grp = document.form1.mailgroup.value;
  29. //alert(grp);
  30. xmlHttp=GetXmlHttpObject()
  31. if (xmlHttp==null)
  32.  {
  33.  alert ("Browser does not support HTTP Request")
  34.  return
  35.  } 
  36. var url="uploader.php"
  37. url=url+"?q="+fileName
  38. url=url+"&sid="+Math.random()
  39. xmlHttp.onreadystatechange=stateChanged 
  40. xmlHttp.open("GET",url,true)
  41. xmlHttp.send(null)
  42.  
  43. }
  44.  
  45. function stateChanged() 
  46. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  47.  { 
  48.  document.getElementById("msg").innerHTML=xmlHttp.responseText 
  49.  } 
  50. }
  51.  
  52. function GetXmlHttpObject()
  53. {
  54. var xmlHttp=null;
  55. try
  56.  {
  57.  // Firefox, Opera 8.0+, Safari
  58.  xmlHttp=new XMLHttpRequest();
  59.  }
  60. catch (e)
  61.  {
  62.  //Internet Explorer
  63.  try
  64.   {
  65.   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  66.   }
  67.  catch (e)
  68.   {
  69.   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  70.   }
  71.  }
  72. return xmlHttp;
  73. }
  74.  
  75. function subject1()
  76. {
  77.     if(document.form1.subject.value == "")
  78.     {
  79.         alert('Subject is missing!');
  80.         return false;
  81.     }
  82.     else 
  83.     { 
  84.         return true;
  85.     }
  86.  
  87. }
  88. </script>
  89.  
  90. </head>
  91.  
  92. <body>
  93. <form enctype="multipart/form-data" name="form1" action="" method="POST">
  94. <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
  95. Choose a file to upload: <input name="uploadedfile" id="uploadedfile" type="file" /><br />
  96. <input type="button" onclick="uploadfile()" value="Upload File" />
  97. </form>
  98. <div id="msg"></div>
  99. </body>
  100. </html>
  101.  
here is the php code for file uploader.php
[PHP]<?php
$q=$_GET["q"];
$target_path = "uploads/";

echo $target_path = $target_path . basename($q);
//$_FILES['uploadedfile']['tmp_name'];
//$_FILES['uploadedfile']['tmp_name'];
$p = 'C:\Program Files\xampp\tmp\php168.tmp';
if(move_uploaded_file($p, $target_path)) {
echo "The file ". basename($q).
" has been uploaded";
} else{
echo "<br>There was an error uploading the file, please try again!";
}
?>[/PHP]

please help me to figure out the problem
Apr 19 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.