Как только он завершает создание первого zip-архива для того же каталога или файла, он начинает создавать zip-архив для снова тот же каталог и файл и в конечном итоге перезаписывает предыдущий zip-архив.
Эта проблема заключалась в одновременном создании нескольких zip-архивов для одного и того же каталога или файла, как показано на прикрепленном изображении. Однако с недавними изменениями мы свели эту проблему к созданию двух zip-файлов.
Введите здесь описание изображения
Я создал файл журнала для подробного анализа результатов, и результаты ниже.
Код: Выделить всё
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Код: Выделить всё
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
Подробнее здесь: [url]https://stackoverflow.com/questions/78534798/problem-creating-two-zip-files-when-zipping-the-directory-using-the-exec-functio[/url]
Мобильная версия