Problem creating two zip files when zipping the directory using the Exec function

I am trying to zip the directory or file using the Exec function, but there is a problem.
As soon as it finishes creating the first zip for the same directory or file, it starts creating the zip for the same directory and file again and eventually overwrites the previous zip.
This problem was creating multiple zips for the same directory or file at the same time, as seen in the attached image. However, with recent changes we have reduced this problem to creating two zip files.
enter image description here

I created a log file to analyze the results in detail and the results are below.
2024-05-26 11:58:30 - Lock file is being created: /tmp/32f20e7fedb75ac2b1ebd53199bf7486.lock 2024-05-26 11:58:30 - Running zip command: /home/user/ZIP/file_name-2024-05-26-11-58-30.zip 2024-05-26 11:58:52 - Zip Archive Created Successfully: /home/user/ZIP/file_name-2024-05-26-11-58-30.zip 2024-05-26 11:58:52 - Lock file deleted: /tmp/32f20e7fedb75ac2b1ebd53199bf7486.loc

Below is my zip creation function code.
` function zipDataUsingSystem($source, $destination, $comment = ”) {
$zipsonuc = [];

    // Check if source directory or file exists
    if (!file_exists($source)) {
        $zipsonuc[] = "Source file or directory does not exist: " . $source;
        return $zipsonuc;
    }

    // Processing and securing file paths
    $sourceRealPath = realpath($source);
    $destinationSafe = escapeshellarg($destination); // Make it safe for command only

    // Specify the path to the lock file
    $lockFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . md5($sourceRealPath) . '.lock';

    // Write to the log file that the lock file was created
    file_put_contents('/home/user/error.log', date('Y-m-d H:i:s') . " - Lock file is being created: " . $lockFile . "n", FILE_APPEND);

    // Create and lock lock file
    $fp = fopen($lockFile, 'c');
    if (!$fp) {
        $zipsonuc[] = "Lock file could not be created: " . $lockFile;
        return $zipsonuc;
    }

    // Lock operation
    if (!flock($fp, LOCK_EX | LOCK_NB)) {
        fclose($fp);
        $zipsonuc[] = "Another zip process in progress: " . $source;
        // Write to the log file that it could not be locked.
        file_put_contents('/home/user/error.log', date('Y-m-d H:i:s') . " - Another zip process in progress: " . $source . "n", FILE_APPEND);
        return $zipsonuc;
    }

    // Check if the target directory exists and create it if necessary
    $destinationDirRealPath = dirname($destination);
    if (!file_exists(KOKYOLU.$destinationDirRealPath)) {
        if (!mkdir(KOKYOLU.$destinationDirRealPath, 0777, true)) {
            $zipsonuc[] = "Could not create target directory: " . $destinationDirRealPath;
            // Release the lock and close the lock file
            flock($fp, LOCK_UN);
            fclose($fp);
            // Write to the log file that the directory could not be created
            file_put_contents('/home/user/error.log', date('Y-m-d H:i:s') . " - Could not create target directory: " . $destinationDirRealPath . "n", FILE_APPEND);
            return $zipsonuc;
        }
    }

    // Write in the log file that the zip command will be run
    file_put_contents('/home/user/error.log', date('Y-m-d H:i:s') . " - Running zip command: " . $destination . "n", FILE_APPEND);

    // Create the zip command, go into the source directory and add its contents
    $command = "cd " . escapeshellarg($sourceRealPath) . " && zip -r $destinationSafe .";
    $output = [];
    $return_var = 0;
    exec($command, $output, $return_var);

    // Check the results
    if ($return_var === 0) {
        // Comment adding process
        if ($comment !== '') {
            $comment = escapeshellarg(iconv(mb_detect_encoding($comment, mb_detect_order(), true), "UTF-8", $comment));
            $commentCommand = "zip -z $destinationSafe <<< $comment";
            exec($commentCommand);
        }

        // Remove single quotes in original filename
        $destinationClean = str_replace("'", "", $destination);

        $zipsonuc[] = "Zip Archive Created Successfully";
        $zipsonuc["dosya_adi"] = $destinationClean;

        // Write to the log file that the zip process was completed successfully.
        file_put_contents('/home/user/error.log', date('Y-m-d H:i:s') . " - Zip Archive Created Successfully: " . $destinationClean . "n", FILE_APPEND);

    } else {
        $zipsonuc[] = "Zip Archive Could Not Be Created Due to an Error: " . implode("<br>", $output);
        // Write the error message to the log file
        file_put_contents('/home/user/error.log', date('Y-m-d H:i:s') . " - Zip Archive Could Not Be Created Due to an Error: " . implode(", ", $output) . "n", FILE_APPEND);
    }

    // Release the lock and close the lock file
    flock($fp, LOCK_UN);
    fclose($fp);
    unlink($lockFile); // Delete lock file

    // Write to the log file that the lock file has been deleted
    file_put_contents('/home/user/error.log', date('Y-m-d H:i:s') . " - Lock file deleted: " . $lockFile . "n", FILE_APPEND);

    return $zipsonuc;
}`

Your help will be appreciated
Thank you from now