Have problem with while cycle in php script

Okay i'm using else function in this code ,i think it causes the problem.But not sure how to fix it.The error as follows

Notice: Undefined variable: STR in C:\\www\testas.php on line 24

it creates empty .txt file instead of parsed one.
<?
error_reporting(E_ALL);
$source = $_POST['source'];
$output = $_POST['output'];
$first = $_POST['first'];
$second = $_POST['second'];
 
if((empty($source))OR(empty($output))OR(empty($first))OR(empty($second))) { $output= "NO FILE"; }  else {
 
$contents=file_get_contents($source);
 
$c1= $first;
$c2= $second;
 
$html = preg_replace("/\n/"," ",$contents);
 
preg_match_all("/(.*?)<td class=\"td2\" nowrap=\"nowrap\"><b>(.*?)<\/b><\/td>(.*?)/",$html,$tags);
 
while(list($key,$val)=each($tags[2])){
  $STR.=$c1."".$val."".$c2."\n";
}
 
$fp=fopen($output,"a+");
fwrite($fp,"$STR");
fclose($fp);
}
?>

Open in new window

olivestAsked:
Who is Participating?
 
olivestConnect With a Mentor Author Commented:
Okay Don't matter FIXED it attached the source code if any one needs it
<?
//***** IP Address Extractor *****
//****Credits to nFinite[GPU]*****
//*** nfinitegpu@hotmail.com *****
 
$source = $_FILES['source']["name"];;
$output = $_POST['output'];
$first = $_POST['first'];
$second = $_POST['second'];
if((empty($source))OR(empty($output))OR(empty($first))OR(empty($second))) { $output=""; }  else {
 
$target_path = "C:/TestSrv/AppServ/www/";
$target_path = $target_path . basename( $_FILES['source']['name']); 
move_uploaded_file($_FILES["source"]["tmp_name"], $target_path);
$srcfl = $_FILES["source"]["name"];
$contents=file_get_contents($srcfl);
 
$c1= $first;
$c2= $second;
 
$html = preg_replace("/\n/"," ",$contents);
 
preg_match_all('/(.*?)(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' .
     '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' .
     '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' .
     '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)(.*?)/',
     $html,$tags);
 
while(list($key,$val)=each($tags[2])){
  $STR.=$c1."".$val."".$c2."\n";
}
 
$fp=fopen($output,"a+");
fwrite($fp,"$STR");
fclose($fp);
}
?>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IP Extractor</title>
<style type="text/css">
<!--
.style1 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-weight: bold;
	font-size: 10px;
	color: #999999;
}
.style2 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-weight: bold;
	font-size: 12px;
}
.style5 {color: #0066CC}
.style6 {
	color: #0066CC;
	font-weight: bold;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
}
-->
</style>
</head>
 
<body>
<form enctype="multipart/form-data" id="ipforma" name="ipforma" method="post" action="#">
  <label> <span class="style2">  <span class="style5">Please enter file to parse</span>
  <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
  </span></label>
  <table width="350" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="132" class="style1" scope="row">Source File Name</th>
    <td width="218"><input name="source" type="file" class="style1" id="source" value="savas.html" hspace="0" vspace="0" border="0"/></td>
  </tr>
  <tr>
    <th scope="row"><span class="style1">Output File Name</span></th>
    <td><label>
      <input name="output" type="text" class="style1" id="output" value="done.txt" hspace="0" vspace="0" border="0" />
    </label></td>
  </tr>
  <tr>
    <th class="style1" scope="row">First Ln</th>
    <td><label>
      <input name="first" type="text" class="style1" id="first" value="call n " hspace="0" vspace="0" border="0" />
    </label></td>
  </tr>
  <tr>
    <th class="style1" scope="row">Second Ln</th>
    <td><label>
      <input name="second" type="text" class="style1" id="second" value=" &gt;&gt; 130.110.log" hspace="0" vspace="0" border="0"/>
    </label></td>
  </tr>
  <tr>
    <th scope="row">&nbsp;</th>
    <td><label>
      <input name="Extract" type="submit" class="style2" id="Extract" value="Extract" />
    </label></td>
  </tr>
</table>
 <?php if((empty($output))) 
 { 
  echo('<p class="style6">Status: Idle</p>'); 
 }
 else {
 
 echo('<p class="style6">Status: File Parsed <a href="'.$output.'">'.$output.'</a></p>');
 } ?>
<p><br />
</p>
 
 
</form>
</body>
</html>

Open in new window

0
 
DerkArtsCommented:
It seems that your regex is not working properly. You while loop isnt running because the list is empty.

PLease check de result of your regex en post back.   ( print_r($tags); )

By the way, foreach is much easier here:

foreach($tags[2] as $key=>$val){
  $STR.=$c1."".$val."".$c2."\n";
}

but you still need to check your regex first
0
 
DerkArtsCommented:
Print POST too... That way you can make sure you if else is working properly!

print_r($_POST);
0
 
olivestAuthor Commented:
Okay i've attached full source code and output.Follow up.

Notice: Undefined variable: tags in C:\www\testas.php on line 3
Array ( [MAX_FILE_SIZE] => 100000 [output] => done.txt [first] => call n [second] => >> 130.110.log [Extract] => Extract )
Warning: file_get_contents() expects parameter 1 to be string, array given in C:\www\testas.php on line 13

Notice: Undefined variable: STR in C:\www\testas.php on line 27

<?
error_reporting(E_ALL);
print_r($tags);
print_r($_POST);
$source = $_FILES['source'];
$output = $_POST['output'];
$first = $_POST['first'];
$second = $_POST['second'];
 
if((empty($source))OR(empty($output))OR(empty($first))OR(empty($second))) { $output= "NO FILE"; }  else {
$target_path = ".";
$target_path = $target_path . basename( $_FILES['source']['name']); 
$contents=file_get_contents($source);
 
$c1= $first;
$c2= $second;
 
$html = preg_replace("/\n/"," ",$contents);
 
preg_match_all("/(.*?)<td class=\"td2\" nowrap=\"nowrap\"><b>(.*?)<\/b><\/td>(.*?)/",$html,$tags);
 
foreach($tags[2] as $key=>$val){
  $STR.=$c1."".$val."".$c2."\n";
}
 
$fp=fopen($output,"a+");
fwrite($fp,"$STR");
fclose($fp);
}
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ip Extractor</title>
<style type="text/css">
<!--
.style1 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-weight: bold;
	font-size: 10px;
	color: #999999;
}
.style2 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-weight: bold;
	font-size: 12px;
}
.style5 {color: #0066CC}
.style6 {
	color: #0066CC;
	font-weight: bold;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
}
-->
</style>
</head>
 
<body>
<form enctype="multipart/form-data" id="ipforma" name="ipforma" method="post" action="#">
  <label> <span class="style2">  <span class="style5">Please enter file to parse</span>
  <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
  </span></label>
  <table width="350" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="132" class="style1" scope="row">Source File Name</th>
    <td width="218"><input name="source" type="file" class="style1" id="source" value="savas.html" hspace="0" vspace="0" border="0"/></td>
  </tr>
  <tr>
    <th scope="row"><span class="style1">Output File Name</span></th>
    <td><label>
      <input name="output" type="text" class="style1" id="output" value="done.txt" hspace="0" vspace="0" border="0" />
    </label></td>
  </tr>
  <tr>
    <th class="style1" scope="row">First Ln</th>
    <td><label>
      <input name="first" type="text" class="style1" id="first" value="call n " hspace="0" vspace="0" border="0" />
    </label></td>
  </tr>
  <tr>
    <th class="style1" scope="row">Second Ln</th>
    <td><label>
      <input name="second" type="text" class="style1" id="second" value=" &gt;&gt; 130.110.log" hspace="0" vspace="0" border="0"/>
    </label></td>
  </tr>
  <tr>
    <th scope="row">&nbsp;</th>
    <td><label>
      <input name="Extract" type="submit" class="style2" id="Extract" value="Extract" />
    </label></td>
  </tr>
</table>
  <p class="style6">Output file here <?php echo('<a href="'.$output.'">'.$output.'</a>'); ?> </p>
<p><br />
</p>
</form>
</body>
</html>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.