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

Opening file from folder.

P: 27
Hi,
I an tryin to create a html page where users can choose a file and open it however, i keep getting it wrong.So far this is wat i've worked on:
(open.html)
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Opening file</title>
  4. </head>
  5. <body>
  6. <form  action="display.php" method="POST">
  7. Please choose a file: <input name="saved" type="file" /><br />
  8. <input type="submit" value="Open" />
  9. </form> 
  10. </body>
  11. </html>
and the php codes are:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $target = "upload/"; 
  3. $target = $target . basename( $_FILES['saved']['name']) ; 
  4. $ok=1;
  5.  
  6. if (!($fp=@fopen(basename($_FILES['saved']['name']), "r"))) die ("Couldn't open XML.");
  7. $usercount=0;
  8. $userdata=array();
  9. $state='';
  10.  
  11. function startElementHandler ($parser,$name,$attrib){
  12. global $usercount;
  13. global $userdata;
  14. global $state;
  15.  
  16. switch ($name) {
  17. case $name=="NAME" : {
  18. $userdata[$usercount]["first"] = $attrib["FIRST"];
  19. $userdata[$usercount]["last"] = $attrib["LAST"];
  20. $userdata[$usercount]["nick"] = $attrib["NICK"];
  21. $userdata[$usercount]["title"] = $attrib["TITLE"];
  22. break;
  23. }
  24. case $name=="STREET" : {
  25. $userdata[$usercount]["hstreet"] = $attrib["HOME"];
  26. $userdata[$usercount]["bstreet"] = $attrib["WORK"];
  27. break;
  28. }
  29. case $name=="CITY" : {
  30. $userdata[$usercount]["hcity"] = $attrib["HOME"];
  31. $userdata[$usercount]["bcity"] = $attrib["WORK"];
  32. break;
  33. }
  34. case $name=="COUNTY" : {
  35. $userdata[$usercount]["hcounty"] = $attrib["HOME"];
  36. $userdata[$usercount]["bcounty"] = $attrib["WORK"];
  37. break;
  38. }
  39. case $name=="POSTAL" : {
  40. $userdata[$usercount]["hpostal"] = $attrib["HOME"];
  41. $userdata[$usercount]["bpostal"] = $attrib["WORK"];
  42. break;
  43. }
  44. case $name=="COUNTRY" : {
  45. $userdata[$usercount]["hcountry"] = $attrib["HOME"];
  46. $userdata[$usercount]["bcountry"] = $attrib["WORK"];
  47. break;
  48. }
  49. case $name=="PHONE" : {
  50. $userdata[$usercount]["hphone"] = $attrib["HOME"];
  51. $userdata[$usercount]["bphone"] = $attrib["WORK"];
  52. break;
  53. }
  54. case $name=="FAX" : {
  55. $userdata[$usercount]["hfax"] = $attrib["HOME"];
  56. $userdata[$usercount]["bfax"] = $attrib["WORK"];
  57. break;
  58. }
  59. case $name=="MOBILE" : {
  60. $userdata[$usercount]["hmobile"] = $attrib["HOME"];
  61. $userdata[$usercount]["bmobile"] = $attrib["WORK"];
  62. break;
  63. }
  64. case $name=="WEB" : {
  65. $userdata[$usercount]["hweb"] = $attrib["HOME"];
  66. $userdata[$usercount]["bweb"] = $attrib["WORK"];
  67. break;
  68. }
  69.  
  70. default : {$state=$name;break;}
  71. }
  72. }
  73.  
  74. function endElementHandler ($parser,$name){
  75. global $usercount;
  76. global $userdata;
  77. global $state;
  78. $state='';
  79. if($name=="CONTACT") {$usercount++;}
  80. }
  81.  
  82. function characterDataHandler ($parser, $data) {
  83. global $usercount;
  84. global $userdata;
  85. global $state;
  86. if (!$state) {return;}
  87. if ($state=="COMPANY") { $userdata[$usercount]["bcompany"] = $data;}
  88. if ($state=="GENDER") { $userdata[$usercount]["gender"] = $data;}
  89. if ($state=="BDAY") { $userdata[$usercount]["bday"] = $data;}
  90. if ($state=="ANNI") { $userdata[$usercount]["anni"] = $data;}
  91. if ($state=="SPOUSE") { $userdata[$usercount]["spouse"] = $data;}
  92. if ($state=="CHILDREN") { $userdata[$usercount]["children"] = $data;}
  93. if ($state=="EMAILONE") { $userdata[$usercount]["email1"] = $data;}
  94. if ($state=="EMAILTWO") { $userdata[$usercount]["email2"] = $data;}
  95. if ($state=="EMAILTHREE") { $userdata[$usercount]["email3"] = $data;}
  96. if ($state=="EMAILFOUR") { $userdata[$usercount]["email4"] = $data;}
  97. if ($state=="COMMENT") {$userdata[$usercount]["comment"] = $data;}
  98. }
  99. if (!($xml_parser = xml_parser_create())) die("Couldn't create parser.");
  100. xml_set_element_handler( $xml_parser, "startElementHandler", "endElementHandler");
  101. xml_set_character_data_handler( $xml_parser, "characterDataHandler");
  102.  
  103. while( $data = fread($fp, 4096)){
  104. if(!xml_parse($xml_parser, $data, feof($fp))) {
  105. break;}}
  106. xml_parser_free($xml_parser);
  107.  
  108. ?>
  109. <html>
  110. <head><title>CodeHelp - Testing PHP XML parser</title>
  111. </head>
  112. <body>
  113. <?php
  114. for ($i=0;$i<$usercount; $i++)
  115. {
  116.         echo "Name: ".$userdata[$i]["title"]." ".ucfirst($userdata[$i]["first"])." ". ucfirst($userdata[$i]["last"])."<br>";
  117.  
  118.  
  119.      if ($userdata[$i]["nick"]) {echo "Nickname ".$userdata[$i]["nick"]."<br>";}
  120.  
  121.         if ($userdata[$i]["hstreet"]) {
  122.                 echo "Home Address: ".$userdata[$i]["hstreet"].", ";
  123.                 echo $userdata[$i]["hcity"].", ".$userdata[$i]["hcounty"].", ". $userdata[$i]["hpostal"].
  124.                         ". ".$userdata[$i]["hcountry"]."<br>";
  125.         }
  126.  
  127.         if ($userdata[$i]["hphone"]) {echo "Home Phone ".$userdata[$i]["hphone"]."<br>";}
  128.         if ($userdata[$i]["hfax"]) {echo "Home Fax ".$userdata[$i]["hfax"]."<br>";}
  129.         if ($userdata[$i]["hmobile"]) {echo "Personal Mobile ".$userdata[$i]["hmobile"]."<br>";}
  130.         if ($userdata[$i]["hweb"]) {
  131.                 echo "Personal site: <a href=\"".$userdata[$i]["hweb"]."\">".$userdata[$i]["hweb"]."</a><br>";
  132.         }
  133.  
  134.         if ($userdata[$i]["bstreet"]) {
  135.                 echo "Business Address: ".$userdata[$i]["bstreet"];
  136.                 echo ", ".$userdata[$i]["bcity"].". ".$userdata[$i]["bcounty"].", ". $userdata[$i]["bpostal"].
  137.                         ". ".$userdata[$i]["bcountry"]."<br>";
  138.         }
  139.  
  140.         if ($userdata[$i]["bphone"]) {echo "Work Phone ".$userdata[$i]["bphone"]."<br>";}
  141.         if ($userdata[$i]["bfax"]) {echo "Work Fax ".$userdata[$i]["bfax"]."<br>";}
  142.         if ($userdata[$i]["bmobile"]) {echo "Business Mobile ".$userdata[$i]["bmobile"]."<br>";}
  143.         if ($userdata[$i]["bcompany"]) {echo "Company: ".$userdata[$i]["bcompany"]."<br>";}
  144.         if ($userdata[$i]["gender"]) {echo "Gender: ".$userdata[$i]["gender"]."<br>";}
  145.         if ($userdata[$i]["children"]) {echo "Children: ".$userdata[$i]["children"]."<br>";}
  146.         if ($userdata[$i]["bday"]) {echo "Birthday: ".$userdata[$i]["bday"]."<br>";}
  147.         if ($userdata[$i]["anni"]) {echo "Anniversary: ".$userdata[$i]["anni"]."<br>";}
  148.         if ($userdata[$i]["spouse"]) {echo "Spouse: ".$userdata[$i]["spouse"]."<br>";}
  149.         if ($userdata[$i]["bweb"]) {
  150.                 echo "Business site: <a href=\"".$userdata[$i]["bweb"]."\">".$userdata[$i]["bweb"]."</a><br>";
  151.         }
  152.  
  153.         if ($userdata[$i]["email1"]) {
  154.                 echo "email: <a href=\"mailto:".$userdata[$i]["email1"]."\">".$userdata[$i]["email1"]."</a><br>";
  155.                 if ($userdata[$i]["email2"]) {
  156.                         echo "email: <a href=\"mailto:".$userdata[$i]["email2"]."\">".$userdata[$i]["email2"]."</a><br>";
  157.                         if ($userdata[$i]["email3"]) {
  158.                                 echo "email: <a href=\"mailto:".$userdata[$i]["email3"]."\">".$userdata[$i]["email3"]."</a><br>";
  159.                                 if ($userdata[$i]["email4"]) {
  160.                                         echo "email: <a href=\"mailto:".$userdata[$i]["email4"]."\">".$userdata[$i]["email4"]."</a><br>";
  161.                                 }
  162.                         }
  163.                 }
  164.         }
  165.  
  166.         if ($userdata[$i]["comment"]) {echo "<tr><td colspan=2>Notes: ".$userdata[$i]["comment"]."</td></tr>\n";}
  167.  if ($userdata[$i]["title"]!=null){
  168.  echo "Data type for title = char";
  169.  }
  170.  
  171.  
  172. ?>
  173. </table>
  174.  
  175.  
  176. </body><html>
Hope someone can help.
Aug 10 '07 #1
Share this Question
Share on Google+
5 Replies

P: 6
Hi,
I an tryin to create a html page where users can choose a file and open it however, i keep getting it wrong.So far this is wat i've worked on:
(open.html)
<html>
<head>
<title>Opening file</title>
</head>
<body>
<form action="display.php" method="POST">
Please choose a file: <input name="saved" type="file" /><br />
<input type="submit" value="Open" />
</form>
</body>
</html>

and the php codes are:

<?php
$target = "upload/";
$target = $target . basename( $_FILES['saved']['name']) ;
$ok=1;

if (!($fp=@fopen(basename($_FILES['saved']['name']), "r"))) die ("Couldn't open XML.");
$usercount=0;
$userdata=array();
$state='';

function startElementHandler ($parser,$name,$attrib){
global $usercount;
global $userdata;
global $state;

switch ($name) {
case $name=="NAME" : {
$userdata[$usercount]["first"] = $attrib["FIRST"];
$userdata[$usercount]["last"] = $attrib["LAST"];
$userdata[$usercount]["nick"] = $attrib["NICK"];
$userdata[$usercount]["title"] = $attrib["TITLE"];
break;
}
case $name=="STREET" : {
$userdata[$usercount]["hstreet"] = $attrib["HOME"];
$userdata[$usercount]["bstreet"] = $attrib["WORK"];
break;
}
case $name=="CITY" : {
$userdata[$usercount]["hcity"] = $attrib["HOME"];
$userdata[$usercount]["bcity"] = $attrib["WORK"];
break;
}
case $name=="COUNTY" : {
$userdata[$usercount]["hcounty"] = $attrib["HOME"];
$userdata[$usercount]["bcounty"] = $attrib["WORK"];
break;
}
case $name=="POSTAL" : {
$userdata[$usercount]["hpostal"] = $attrib["HOME"];
$userdata[$usercount]["bpostal"] = $attrib["WORK"];
break;
}
case $name=="COUNTRY" : {
$userdata[$usercount]["hcountry"] = $attrib["HOME"];
$userdata[$usercount]["bcountry"] = $attrib["WORK"];
break;
}
case $name=="PHONE" : {
$userdata[$usercount]["hphone"] = $attrib["HOME"];
$userdata[$usercount]["bphone"] = $attrib["WORK"];
break;
}
case $name=="FAX" : {
$userdata[$usercount]["hfax"] = $attrib["HOME"];
$userdata[$usercount]["bfax"] = $attrib["WORK"];
break;
}
case $name=="MOBILE" : {
$userdata[$usercount]["hmobile"] = $attrib["HOME"];
$userdata[$usercount]["bmobile"] = $attrib["WORK"];
break;
}
case $name=="WEB" : {
$userdata[$usercount]["hweb"] = $attrib["HOME"];
$userdata[$usercount]["bweb"] = $attrib["WORK"];
break;
}

default : {$state=$name;break;}
}
}

function endElementHandler ($parser,$name){
global $usercount;
global $userdata;
global $state;
$state='';
if($name=="CONTACT") {$usercount++;}
}

function characterDataHandler ($parser, $data) {
global $usercount;
global $userdata;
global $state;
if (!$state) {return;}
if ($state=="COMPANY") { $userdata[$usercount]["bcompany"] = $data;}
if ($state=="GENDER") { $userdata[$usercount]["gender"] = $data;}
if ($state=="BDAY") { $userdata[$usercount]["bday"] = $data;}
if ($state=="ANNI") { $userdata[$usercount]["anni"] = $data;}
if ($state=="SPOUSE") { $userdata[$usercount]["spouse"] = $data;}
if ($state=="CHILDREN") { $userdata[$usercount]["children"] = $data;}
if ($state=="EMAILONE") { $userdata[$usercount]["email1"] = $data;}
if ($state=="EMAILTWO") { $userdata[$usercount]["email2"] = $data;}
if ($state=="EMAILTHREE") { $userdata[$usercount]["email3"] = $data;}
if ($state=="EMAILFOUR") { $userdata[$usercount]["email4"] = $data;}
if ($state=="COMMENT") {$userdata[$usercount]["comment"] = $data;}
}
if (!($xml_parser = xml_parser_create())) die("Couldn't create parser.");
xml_set_element_handler( $xml_parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler( $xml_parser, "characterDataHandler");

while( $data = fread($fp, 4096)){
if(!xml_parse($xml_parser, $data, feof($fp))) {
break;}}
xml_parser_free($xml_parser);

?>
<html>
<head><title>CodeHelp - Testing PHP XML parser</title>
</head>
<body>
<?php
for ($i=0;$i<$usercount; $i++)
{
echo "Name: ".$userdata[$i]["title"]." ".ucfirst($userdata[$i]["first"])." ". ucfirst($userdata[$i]["last"])."<br>";


if ($userdata[$i]["nick"]) {echo "Nickname ".$userdata[$i]["nick"]."<br>";}

if ($userdata[$i]["hstreet"]) {
echo "Home Address: ".$userdata[$i]["hstreet"].", ";
echo $userdata[$i]["hcity"].", ".$userdata[$i]["hcounty"].", ". $userdata[$i]["hpostal"].
". ".$userdata[$i]["hcountry"]."<br>";
}

if ($userdata[$i]["hphone"]) {echo "Home Phone ".$userdata[$i]["hphone"]."<br>";}
if ($userdata[$i]["hfax"]) {echo "Home Fax ".$userdata[$i]["hfax"]."<br>";}
if ($userdata[$i]["hmobile"]) {echo "Personal Mobile ".$userdata[$i]["hmobile"]."<br>";}
if ($userdata[$i]["hweb"]) {
echo "Personal site: <a href=\"".$userdata[$i]["hweb"]."\">".$userdata[$i]["hweb"]."</a><br>";
}

if ($userdata[$i]["bstreet"]) {
echo "Business Address: ".$userdata[$i]["bstreet"];
echo ", ".$userdata[$i]["bcity"].". ".$userdata[$i]["bcounty"].", ". $userdata[$i]["bpostal"].
". ".$userdata[$i]["bcountry"]."<br>";
}

if ($userdata[$i]["bphone"]) {echo "Work Phone ".$userdata[$i]["bphone"]."<br>";}
if ($userdata[$i]["bfax"]) {echo "Work Fax ".$userdata[$i]["bfax"]."<br>";}
if ($userdata[$i]["bmobile"]) {echo "Business Mobile ".$userdata[$i]["bmobile"]."<br>";}
if ($userdata[$i]["bcompany"]) {echo "Company: ".$userdata[$i]["bcompany"]."<br>";}
if ($userdata[$i]["gender"]) {echo "Gender: ".$userdata[$i]["gender"]."<br>";}
if ($userdata[$i]["children"]) {echo "Children: ".$userdata[$i]["children"]."<br>";}
if ($userdata[$i]["bday"]) {echo "Birthday: ".$userdata[$i]["bday"]."<br>";}
if ($userdata[$i]["anni"]) {echo "Anniversary: ".$userdata[$i]["anni"]."<br>";}
if ($userdata[$i]["spouse"]) {echo "Spouse: ".$userdata[$i]["spouse"]."<br>";}
if ($userdata[$i]["bweb"]) {
echo "Business site: <a href=\"".$userdata[$i]["bweb"]."\">".$userdata[$i]["bweb"]."</a><br>";
}

if ($userdata[$i]["email1"]) {
echo "email: <a href=\"mailto:".$userdata[$i]["email1"]."\">".$userdata[$i]["email1"]."</a><br>";
if ($userdata[$i]["email2"]) {
echo "email: <a href=\"mailto:".$userdata[$i]["email2"]."\">".$userdata[$i]["email2"]."</a><br>";
if ($userdata[$i]["email3"]) {
echo "email: <a href=\"mailto:".$userdata[$i]["email3"]."\">".$userdata[$i]["email3"]."</a><br>";
if ($userdata[$i]["email4"]) {
echo "email: <a href=\"mailto:".$userdata[$i]["email4"]."\">".$userdata[$i]["email4"]."</a><br>";
}
}
}
}

if ($userdata[$i]["comment"]) {echo "<tr><td colspan=2>Notes: ".$userdata[$i]["comment"]."</td></tr>\n";}
}
if ($userdata[$i]["title"]!=null){
echo "Data type for title = char";
}


?>
</table>


</body><html>

Hope someone can help.
=============================

hai,

Juz try this...... hope it works
Expand|Select|Wrap|Line Numbers
  1. <form  action="display.php" method="POST" enctype="multipart/form-data">
Aug 10 '07 #2

P: 27
I know my error is at the php file.....its not a the html file....i believe the value "saved" is not passed on to the php file or may be the value is passed on but the php file does not grab it....
Aug 10 '07 #3

pbmods
Expert 5K+
P: 5,821
Heya, bimeldip.

Please try to avoid posting so much code; you're more likely to get a response if you only post relevant snippets.

What do you want your code to do? Give an example.
What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Aug 13 '07 #4

pbmods
Expert 5K+
P: 5,821
bimeldip and munnaj, please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Aug 13 '07 #5

P: 27
Hi,
so sorry ...i did not realize that it wasn't allowed.I will keep in mind the next time.
My problem is that i want to develop a php class that has these functions:

1)Upload function
-users can browse for an xml file and click a button and upload it into a folder.

2)Recognize function
-from the file there is a function that will recognize the data type of the content of the file eg:
..
<FirstName>Tom</FirstName>
So it would recognize that Tom is a character so it will display the data type.

3)Import Function
-this function would be recognize the the fields in the xml file, and create a table based on the fields and import the data into the table in the mysql database.

so far i have only managed to learn how to upload the file into a folder.
But the problem is that i have hard code the xml file in my codes for it to display the content of the xml file.
eg:
..
if (!($fp=@fopen("upload/contactsbare.xml", "r"))) die ("Couldn't open XML.");
..
So i want to make it dynamic enough to pick any xml file that users will choose and is able to display it to the user.
How do i do this?

Please do help...
Aug 13 '07 #6

Post your reply

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