Home > OS >  Need help unzip a tmp file and and rewite it to another path
Need help unzip a tmp file and and rewite it to another path

Time:09-15

(I also need help to found a better title but i have no idea how to formulate it)

I will have some issue explain what i need to do because it's pretty hard. Basicly i'm making a monitoring system, i'm getting the log file from a curl command:

curl -F 'file=@/home/jbaudoux/logbackup-infitarif (3).gz'  http://pc-arnaud/projet/index.php?customer=exemple

And my script will take the .gz file, move it from the tmp to the a folder made by the php file C://tmplog/exemple and then unzip it.

But i think i'm making it too hard and doing the work twice. Lemme show you my code:

$tmpFilePath = $_FILES["file"]["tmp_name"];

$buffer_size = 8192;
$out_file_path = $folderPath ."\\" .$_FILES["file"]["name"]; 

$compressedFile = gzopen($tmpFilePath, 'rb');
$out_file = fopen($out_file_path, 'wb'); 

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


fclose($out_file);
gzclose($compressedFile);




$source_file_path = $folderPath ."\\" .$_FILES["file"]["name"];
$buffer_size = 4096;
$out_file_path = str_replace('.gz', '', $source_file_path);
$file = gzopen($source_file_path, 'rb');
$out_file = fopen($out_file_path, 'wb');


$file_as_text = "";
while(!gzeof($file)) {
    $tmp_read = gzread($file, $buffer_size);
    
    fwrite($out_file, $tmp_read);

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

Do you see? I'm doing the stuff twice. Any idea of how to made it work wihout doing that's twice? I hope so.

Thanks for the help! :)

CodePudding user response:

Should do something like this:

$importantpath = $folderPath ."\\" .$_FILES["file"]["name"];
//copie le fichier compressé
$tmpFilePath = $_FILES["file"]["tmp_name"];
$buffer_size = 8192;
$out_file_path = $importantpath; 
$compressedFile = gzopen($tmpFilePath, 'rb');

$out_file_pathe = str_replace('.gz', '', $out_file_path);
$out_file = fopen($out_file_pathe, 'wb');

$file = gzopen($out_file_pathe, 'rb');
$out_filee = fopen($out_file_pathe, 'wb');

$file_as_text = "";
if($compressedFile != false)
{
    while (!gzeof($compressedFile)){
        fwrite($out_file, gzread($compressedFile, $buffer_size)); 
        $tmp_read = gzread($file, $buffer_size);
        fwrite($out_filee, $tmp_read);
    }
}
else{
    print("Attention probleme file false");
}

fclose($out_file);
fclose($out_filee);
gzclose($compressedFile);
gzclose($file);
  • Related