-1

I'm trying to make a script work which is going to be used as a monitoring system, but I got a problem Error saying:

Notice: Undefined variable: keep_for_later line 47

but this variable is set line 39.

Any idea?

Here is my php:

<?php
print_r($_FILES["file"]);
$file_name = $_FILES["file"]["tmp_name"];

$buffer_size = 4096;
$out_file_name = str_replace('gz', '', $file_name);

$file = gzopen($file_name, 'rb');
$out_file = fopen($out_file_name, 'wb');

if($file != false)
{
    while (!gzeof($file)){
        fwrite($out_file, gzread($file, $buffer_size));
    }
}
else{
    print("Attention problem file false");
}


fclose($out_file);
gzclose($file);

// $_FILES["file"]["name"]

if (unlink("C:\\tmplog\\".$_FILES["file"]["name"]. "/user.log")) {
    // file was successfully deleted
  } else {
    // there was a problem deleting the file
  }
  sleep(2);
  $find_str = 'total size is';
  $fp = @fopen($_FILES["file"]["tmp_name"], "r");
  if ($fp) {
      while (($line = fgets($fp,)) !== false) {
          if ( strpos($line, $find_str) !== false ) {
              $keep_for_later = $line;
          }
      }
      fclose($fp);
  }



print($keep_for_later);
if (!file_exists('C:\tmplog')) {
    mkdir('C:\tmplog', 0755, true);
}

if (!file_exists("C:\\tmplog\\".$_FILES["file"]["name"])) {
    mkdir("C:\\tmplog\\".$_FILES["file"]["name"], 0755, true);
}

$dir = "C:\\tmplog\\".$_FILES["file"]["name"];
file_put_contents($dir ."/user.log", $keep_for_later);




$array = array();
$value = preg_match("/(\d*,\d*|\d+)/",$keep_for_later, $array);
print_r($array);

if($array){
    $teste = $array[0];
    $testa = str_replace(',','', $teste);
    if(is_numeric($teste)){
        $teste = $testa;
    }
    
    
    if ($testa >0) {
        print("Succes: la taille du fichier est de: " .$array[0]);
    }
    else if ($testa == 0) {
        print("Error");
    }
    else{
        print("Warning: the size is: " .$array[0]);
    }
}
else
{

}


$dir = "C:\\tmplog\\".$_FILES["file"]["name"];
file_put_contents($dir ."/result.log", $array);

I got the file from a Curl command:

curl -F 'file=@logbackup-rt.gz'  http://mylink/projet/index.php

Which is run on Linux.

Any idea? Thanks if so!

D4rk_Devs
  • 13
  • 12
  • 1
    If the file does not contain the "total size is" string, that variable will never be created. [Check the other answer I gave](https://stackoverflow.com/questions/73631784/stream-get-line-how-to-set-where-it-starting-to-read-the-file) and initialise the variable before that section of code – RiggsFolly Sep 08 '22 at 15:08
  • 1
    But you probably ought to wonder why the search string is missing, and amend the code to cope elegantly with that situation – RiggsFolly Sep 08 '22 at 15:10
  • What have you tried to resolve the problem? Where are you stuck? – Nico Haase Sep 15 '22 at 12:30

1 Answers1

1

That is saying the total size is was not found.
Is the case correct? Like is Total capitalized?
strpos is case sensitive. stripos is not.

Set $keep_for_later to a value above the unlink line.

$keep_for_later = 'Not Found';
if (unlink("C:\\tmplog\\".$_FILES["file"]["name"]. "/user.log")) {
Misunderstood
  • 5,534
  • 1
  • 18
  • 25