This is to upload and print a text file. Now, this txt file contains details of people booking and comes via email and looks something like this:
some text (always the same)
= ================
text again
name : Mr F Someone
address: Somewhere
...
...
more same text
The problem is there are no delimiters. and I want to grab the data like "Mr", "F", "Someone",... and then put each one of them in an input field of a form. I tried something like this:
$section_a = file_get_contents($up_folder, NULL, NULL, 508, 2);
echo '<input type="text" name="Title" value="'.$section_a.'">';
but if it is "Miss" instead of "Mr" then it is a problem, same as the length of the name and so on. And there is more than one "booking" in a file. So, is there a way of getting the details of each booking out of the file to put each time in a form?
<?php
if($_FILES['userfile']['error'] > 0){
echo 'Houston, we have a problem: ';
switch($_FILES['userfile']['error'])
{
case 1: echo 'file too big'; break;
case 2: echo 'file too big'; break;
case 3: echo 'incomplete upload'; break;
case 4: echo 'uplad error'; break;
}
exit;
}
if($_FILES['userfile']['type'] != 'text/plain'){
echo 'Ooh noes! File is not plain text dude!';
exit;
}
$up_folder = 'upload/'.$_FILES['userfile']['name'];
if(is_uploaded_file($_FILES['userfile']['tmp_name'])){
if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $up_folder)){
echo 'cannot move file';
exit;
}
}
else{
echo 'error: ';
echo $_FILES['userfile']['name'];
exit;
}
echo 'file upload successful<br>';
$fp = fopen($up_folder, 'r');
$contents = fread($fp, filesize($up_folder));
fclose($fp);
$contents = strip_tags($contents);
$fp = fopen($upfile, 'w');
fwrite($fp, $contents);
fclose($fp);
echo 'Uploaded file content: <br> <br>';
echo $contents;
?>
$fetch = ['name','address'];
$output = [];
$fp = fopen($file, 'r');
while(!feof($fp) && count($output) !== count($fetch)) {
$line = fgets($line);
list($prefix, $value) = explode(':', $line, 2);
$prefix = strtolower(trim($prefix));
foreach($fetch as $f) {
if($prefix === $f) {
$output[$f] = trim($value);
break;
}
}
}
fclose($fp);
print_r($output);