代码之家  ›  专栏  ›  技术社区  ›  adam78

PHP-ZipArchive close返回true,尽管没有添加任何文件

  •  0
  • adam78  · 技术社区  · 7 年前

    我有以下代码来创建zip存档。如果我真的创建了档案怎么查?

    当没有添加文件时调用 close

     $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 回复  |  直到 7 年前
        1
  •  0
  •   adam78    7 年前

    为了让代码正常工作,我只需按如下方式重构代码:

    $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),
          ]);
     }
    
        2
  •  0
  •   apokryfos    7 年前

    如果每个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),
              ]);
           }
    
     }