Jump to content

Recommended Posts

hi, just wondered if there was a way of parsing a text file using a script such as the one below without having to hardcode the filename and instead specifying the file using:

 

<input type="file" id="file" name="file" />

 

as all the scripts I've seen know the filename beforehand whereas in my situation I won't know the filename but will know the format of the contents of the file such as what I need to parse. If this can't be done then I guess the user will have to upload their text file and then once it's uploaded parse it from from the location in my web space as I will then know where the text file is? Thanks in advance. Also each bit of information in the text file is seperated by tabs and for each line I only wish to parse the first 6 bits of information e.g.:

 

UK 501074 600019 1994-10-01 F LMX DONT WISH TO PARSE

UK 520678 100092 1992-08-01 F SMX DONT WISH TO PARSE

 

----------------------------------------------

 

<?php

$filename = "test.txt";

$handle = fopen($filename, "rb");

$contents = fread($handle, filesize($filename));

echo $contents;

fclose($handle);

?>

Nobody can help? Everyone knows the location and filename of the file they wish to parse beforehand? I'm sure there must be a way of capturing the location and filename of a file when someone selects it using <input type="file" />? Am I wrong? Any help would be much appreciated! Thanks.

More information would be nice. How is the file gathered? If it is not uploaded to the server how do you have access to that file? If it is uploaded to your server, than I am sure you can get the name that way.

 

If they are just uploading a file and you do not want to store it, you can grab the data of the file "on the fly" without even needing the filename to begin with. It seems your question can be answered by this site http://us2.php.net/manual/en/features.file-upload.php and reading through that manual.

 

Good reading.

 

--FrosT

The text file is gathered from another website which contains all the cattle a farmer has registered with the government. Now for ease, rather than the farmer then having to enter all this animal data again one by one in my website, I thought it would be good for them to be able to load their animal data by parsing the text file they can download from the other site of all their cattle. As I said in my first post I thought I would have to upload the text file in order to do what I want to do but uploading it without storing it yet gathering the contents of the file would be perfect so thanks for clarifying that. I am reading the manual now but I see no examples of grabbing the data of the file "on the fly".  ??? Thanks. P.S. as of yet no output, not that far yet!

Instead of moving the uploaded file to a permanent location, you read it from where it was uploaded to:

 

For your file input file use the code:

 

<?php
$filename = $_FILES['file']['tmp_name'];
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
echo $contents;
fclose($handle);
?>

Thanks for all your help, here's the final thing which parses a text file on the fly as well as reading only the first 6 bits of data from each line of the file which is tab delimited and has no \n to show a new line.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
    <title>Upload</title>
</head>
<body>
<?php
if(!isset($_POST['submit'])){
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" id="submit" value="Parse" />
</form>';
} else {
	if ((($_FILES["file"]["type"] == "text/plain")) && ($_FILES["file"]["size"] < 1024)) {
        	if ($_FILES["file"]["error"] > 0) {
            	echo "Error: " . $_FILES["file"]["error"] . "<br />";
        	} else {
			$filename = $_FILES['file']['tmp_name'];
			$handle = fopen($filename, "rb");
			while (!feof($handle)) {
				$data = fscanf($handle, "%s%s%s%s%s%s");
				if($data) {
					list ($country_of_origin, $herd_id, $cattle_id, $dob, $sex, $breed) = $data;
					echo "<p>";
					echo $country_of_origin . " | ";
					echo $herd_id . " | ";
					echo $cattle_id . " | ";
					echo $dob . " | ";
					echo $sex . " | ";
					echo $breed;
					echo "</p>";
				}
				$data=NULL;
			}
			fclose($handle);
		}
    	} else {
        	echo "Invalid file";
    	}
}
?>
</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.