Opening file from folder.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bimeldip
    New Member
    • Aug 2007
    • 27

    #1

    Opening file from folder.

    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)[code=html]
    <html>
    <head>
    <title>Openin g 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>[/code]

    and the php codes are:
    [code=php]
    <?php
    $target = "upload/";
    $target = $target . basename( $_FILES['saved']['name']) ;
    $ok=1;

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

    function startElementHan dler ($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;b reak;}
    }
    }

    function endElementHandl er ($parser,$name) {
    global $usercount;
    global $userdata;
    global $state;
    $state='';
    if($name=="CONT ACT") {$usercount++;}
    }

    function characterDataHa ndler ($parser, $data) {
    global $usercount;
    global $userdata;
    global $state;
    if (!$state) {return;}
    if ($state=="COMPA NY") { $userdata[$usercount]["bcompany"] = $data;}
    if ($state=="GENDE R") { $userdata[$usercount]["gender"] = $data;}
    if ($state=="BDAY" ) { $userdata[$usercount]["bday"] = $data;}
    if ($state=="ANNI" ) { $userdata[$usercount]["anni"] = $data;}
    if ($state=="SPOUS E") { $userdata[$usercount]["spouse"] = $data;}
    if ($state=="CHILD REN") { $userdata[$usercount]["children"] = $data;}
    if ($state=="EMAIL ONE") { $userdata[$usercount]["email1"] = $data;}
    if ($state=="EMAIL TWO") { $userdata[$usercount]["email2"] = $data;}
    if ($state=="EMAIL THREE") { $userdata[$usercount]["email3"] = $data;}
    if ($state=="EMAIL FOUR") { $userdata[$usercount]["email4"] = $data;}
    if ($state=="COMME NT") {$userdata[$usercount]["comment"] = $data;}
    }
    if (!($xml_parser = xml_parser_crea te())) die("Couldn't create parser.");
    xml_set_element _handler( $xml_parser, "startElementHa ndler", "endElementHand ler");
    xml_set_charact er_data_handler ( $xml_parser, "characterDataH andler");

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

    ?>
    <html>
    <head><title>Co deHelp - Testing PHP XML parser</title>
    </head>
    <body>
    <?php
    for ($i=0;$i<$userc ount; $i++)
    {
    echo "Name: ".$userdata[$i]["title"]." ".ucfirst($user data[$i]["first"])." ". ucfirst($userda ta[$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=\"".$userd ata[$i]["hweb"]."\">".$userdat a[$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 "Anniversar y: ".$userdata[$i]["anni"]."<br>";}
    if ($userdata[$i]["spouse"]) {echo "Spouse: ".$userdata[$i]["spouse"]."<br>";}
    if ($userdata[$i]["bweb"]) {
    echo "Business site: <a href=\"".$userd ata[$i]["bweb"]."\">".$userdat a[$i]["bweb"]."</a><br>";
    }

    if ($userdata[$i]["email1"]) {
    echo "email: <a href=\"mailto:" .$userdata[$i]["email1"]."\">".$userdat a[$i]["email1"]."</a><br>";
    if ($userdata[$i]["email2"]) {
    echo "email: <a href=\"mailto:" .$userdata[$i]["email2"]."\">".$userdat a[$i]["email2"]."</a><br>";
    if ($userdata[$i]["email3"]) {
    echo "email: <a href=\"mailto:" .$userdata[$i]["email3"]."\">".$userdat a[$i]["email3"]."</a><br>";
    if ($userdata[$i]["email4"]) {
    echo "email: <a href=\"mailto:" .$userdata[$i]["email4"]."\">".$userdat a[$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>[/code]

    Hope someone can help.
  • munnaj
    New Member
    • Jul 2007
    • 6

    #2
    Originally posted by bimeldip
    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>Openin g 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(b asename($_FILES['saved']['name']), "r"))) die ("Couldn't open XML.");
    $usercount=0;
    $userdata=array ();
    $state='';

    function startElementHan dler ($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;b reak;}
    }
    }

    function endElementHandl er ($parser,$name) {
    global $usercount;
    global $userdata;
    global $state;
    $state='';
    if($name=="CONT ACT") {$usercount++;}
    }

    function characterDataHa ndler ($parser, $data) {
    global $usercount;
    global $userdata;
    global $state;
    if (!$state) {return;}
    if ($state=="COMPA NY") { $userdata[$usercount]["bcompany"] = $data;}
    if ($state=="GENDE R") { $userdata[$usercount]["gender"] = $data;}
    if ($state=="BDAY" ) { $userdata[$usercount]["bday"] = $data;}
    if ($state=="ANNI" ) { $userdata[$usercount]["anni"] = $data;}
    if ($state=="SPOUS E") { $userdata[$usercount]["spouse"] = $data;}
    if ($state=="CHILD REN") { $userdata[$usercount]["children"] = $data;}
    if ($state=="EMAIL ONE") { $userdata[$usercount]["email1"] = $data;}
    if ($state=="EMAIL TWO") { $userdata[$usercount]["email2"] = $data;}
    if ($state=="EMAIL THREE") { $userdata[$usercount]["email3"] = $data;}
    if ($state=="EMAIL FOUR") { $userdata[$usercount]["email4"] = $data;}
    if ($state=="COMME NT") {$userdata[$usercount]["comment"] = $data;}
    }
    if (!($xml_parser = xml_parser_crea te())) die("Couldn't create parser.");
    xml_set_element _handler( $xml_parser, "startElementHa ndler", "endElementHand ler");
    xml_set_charact er_data_handler ( $xml_parser, "characterDataH andler");

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

    ?>
    <html>
    <head><title>Co deHelp - Testing PHP XML parser</title>
    </head>
    <body>
    <?php
    for ($i=0;$i<$userc ount; $i++)
    {
    echo "Name: ".$userdata[$i]["title"]." ".ucfirst($user data[$i]["first"])." ". ucfirst($userda ta[$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=\"".$userd ata[$i]["hweb"]."\">".$userdat a[$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 "Anniversar y: ".$userdata[$i]["anni"]."<br>";}
    if ($userdata[$i]["spouse"]) {echo "Spouse: ".$userdata[$i]["spouse"]."<br>";}
    if ($userdata[$i]["bweb"]) {
    echo "Business site: <a href=\"".$userd ata[$i]["bweb"]."\">".$userdat a[$i]["bweb"]."</a><br>";
    }

    if ($userdata[$i]["email1"]) {
    echo "email: <a href=\"mailto:" .$userdata[$i]["email1"]."\">".$userdat a[$i]["email1"]."</a><br>";
    if ($userdata[$i]["email2"]) {
    echo "email: <a href=\"mailto:" .$userdata[$i]["email2"]."\">".$userdat a[$i]["email2"]."</a><br>";
    if ($userdata[$i]["email3"]) {
    echo "email: <a href=\"mailto:" .$userdata[$i]["email3"]."\">".$userdat a[$i]["email3"]."</a><br>";
    if ($userdata[$i]["email4"]) {
    echo "email: <a href=\"mailto:" .$userdata[$i]["email4"]."\">".$userdat a[$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
    [code=html]
    <form action="display .php" method="POST" enctype="multip art/form-data">[/code]

    Comment

    • bimeldip
      New Member
      • Aug 2007
      • 27

      #3
      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....

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        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.

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          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.

          Comment

          • bimeldip
            New Member
            • Aug 2007
            • 27

            #6
            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>To m</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.xm l", "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...

            Comment

            Working...