473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

PHP Ajax file upload script problem

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
0 2293

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

Similar topics

12
by: JMB | last post by:
Hello, I was wondering if anyone knew of any projects extending the inline upload progress bar to utilize an inpage image uploader with bar, without having to refresh or go to a seperate page,...
3
by: markus.rietzler | last post by:
i want to do (multiple) file upload(s) and display a progress bar. with firefox and safari it is no problem at all. only IE makes some problems. my script is based on ajax-uploader, which can be...
3
by: Ken1 | last post by:
Hello, Does anyone know of an easy to implement ajax upload script for php which also has a progress bar. If possible I'd like it to be able to remove already uploaded files and do minor...
3
by: dreamznatcher | last post by:
Hello, I found a script here: http://www.webtoolkit.info/ajax-file-upload.html which supposedly allows you to upload files using AJAX (I'm not an expert). The site claims it's the best way to...
1
by: bsprogs | last post by:
I am currnetly programming a file hosting website in PHP and I am slowly integrating AJAX into the website. Here is my problem: The user uploads the file. The server processes the file and...
3
by: kksandeep | last post by:
i am using this three files to uplod file. i got this file from net but i think these have some error. i am new to this field plz help the script i found is some helpful but not too that i need ...
1
by: kksandeep | last post by:
i am using this three files to uplod file. i got this file from net but i think these have some error. i am new to this field plz help the script i found is some helpful but not too that i need ...
1
by: wenijah | last post by:
Hi everyone! First thank you for reading this post and yes, you probably already see that kind of topic title somewhere but the problem I've got today might be different than the 100 topics I've...
8
by: gearoid | last post by:
Hey I've designed an intranet site where users can upload documents (called 'resources') as part of a knowledge base. Part of the functionality of said site is that privilaged users can edit the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.