PHP - ZipArchive close возвращает true, несмотря на то, что файлы не добавляются - PullRequest
0 голосов
/ 02 января 2019

У меня есть следующий код для создания zip-архива.Как я могу проверить, действительно ли был создан архив?

Когда файлы не добавлены, вызов close все еще возвращает true?

 $profiles = $profileRepository->getProfiles()->get();

 $fileName = time() . '-' . uniqid() . '-' .  '.zip';

 $filePath = storage_path('app/bulkdownloads/' . $fileName);

 $zip = new ZipArchive;

 if ($zip->open($filePath, ZipArchive::CREATE) === TRUE) {

        foreach($profiles as $profile) {
            if (file_exists(storage_path('app/' . $profile->file_path)))
                $zip->addFile(storage_path('app/' . $profile->file_path), $profile->name . '-' . $profile->id . '.' . pathinfo($profile->file_path, PATHINFO_EXTENSION));
        }

        $res = $zip->close();


       if ($res == true) { 
          $newFile = \App\File::create([
            "name"          => $fileName
            "path"          => 'bulkdownloads/' . $fileName,
            "size"          => filesize($filePath),
            "mime_type"     => mime_content_type($filePath),
          ]);
       }

 }

Ответы [ 2 ]

0 голосов
/ 02 января 2019

Каждая операция zip возвращает логическое значение, если оно работает, поэтому вам необходимо проверить все из них:

$profiles = $profileRepository->getProfiles()->get();

 $fileName = time() . '-' . uniqid() . '-' .  '.zip';

 $filePath = storage_path('app/bulkdownloads/' . $fileName);

 $zip = new ZipArchive;

 if ($zip->open($filePath, ZipArchive::CREATE) === TRUE) {
        $res = true;
        foreach($profiles as $profile) {
            if (file_exists(storage_path('app/' . $profile->file_path)))
                $res = $res && $zip->addFile(storage_path('app/' . $profile->file_path), $profile->name . '-' . $profile->id . '.' . pathinfo($profile->file_path, PATHINFO_EXTENSION));
             else 
                $res = false;
        }

        $res = $res && $zip->close();


       if ($res == true) { 
          $newFile = \App\File::create([
            "name"          => $fileName
            "path"          => 'bulkdownloads/' . $fileName,
            "size"          => filesize($filePath),
            "mime_type"     => mime_content_type($filePath),
          ]);
       }

 }
0 голосов
/ 02 января 2019

Мне просто пришлось провести рефакторинг кода следующим образом, чтобы он заработал:

$profiles = $profileRepository->getProfiles()->get();

$fileName = time() . '-' . uniqid() . '-' .  '.zip';

$filePath = storage_path('app/bulkdownloads/' . $fileName);

$zip = new ZipArchive;

if ($zip->open($filePath, ZipArchive::CREATE) === TRUE) {

    foreach($profiles as $profile) {
        if (file_exists(storage_path('app/' . $profile->file_path)))
            $zip->addFile(storage_path('app/' . $profile->file_path), $profile->name . '-' . $profile->id . '.' . pathinfo($profile->file_path, PATHINFO_EXTENSION));
    }

    $zip->close();

}

if (file_exists($filePath)) { 
      $newFile = \App\File::create([
        "name"          => $fileName
        "path"          => 'bulkdownloads/' . $fileName,
        "size"          => filesize($filePath),
        "mime_type"     => mime_content_type($filePath),
      ]);
 }
...