question on the php code please!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lka527
    New Member
    • Apr 2010
    • 15

    question on the php code please!!

    Hi all,
    I have a question about the code.
    I am working on this code to let others to upload their text files in the same format. (having category name and display the answer after : (colon)..
    So, I am trying to let people to be able to upload their text file and go through the code that filters (parses) out the contents I want (what I want is showing the contents after the colon..) Can anyone please help me out?
    Code:
    <?php
     $fileName = $_FILES['fileHandle']['name'];
    
    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
      "C:/upload/" . $_FILES["fileToUpload"]["name"]);
    
     if ($_FILES["fileToUpload"]["error"] > 0)
        {
        echo "Apologies, an error has occurred.";
        echo "Error Code: " . $_FILES["fileToUpload"]["error"];
        }
     else
        {
    
        move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
    	  "C:/upload/" . $_FILES["fileToUpload"]["name"]);
    	}
    
    
      //  $file = fopen($fileName, "r") or exit ("Unable to open file!");
    
    //starting here to parse out the contents of the file..
    
    while(!feof($file)) 
        $line = fgets($file); // Read a line.
    
     
    //escape if the line is empty
            if (trim($line) == "")
                continue;
    //explode from the :
            $fields = explode(":", $line, 2);
     
            echo "<b>$fields[0]</b>";
     
    //checks if second part exists 
            if (isset($fields[1]))
                echo " : $fields[1]";
     
            echo "<br/>";
    
        }
     
        fclose($file);
    ?>
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    Line #24 of your code.
    If you want to put multiple lines inside a while loop, they need to be contained within curly-brackets.
    [code=php]<?php
    $i = 1;
    while($i <= 5) {
    echo "$i";
    echo ">";
    }
    // Prints: 1>2>3>4>5>
    ?>[/code]

    If you don't put all the lines into curly-brackets, only the first line immediately after the loop will be included.
    [code=php]<?php
    $i = 1;
    while($i <= 5)
    echo "$i";
    echo ">";
    // Prints: 12345>
    ?>[/code]

    Comment

    Working...