2017-03-03 84 views
-1

解壓文件,我不知道它的內容的名稱。我需要立即打開並處理這個文件。我怎樣才能抓住它?不知道它的名字?如何獲取我剛剛解壓縮的文件。 PHP

繼承人我的代碼...

if($extension == 'ZIP' || $extension == 'zip'){ 
    $zip = new ZipArchive; 
    $res = $zip->open($_FILES['my_file']['tmp_name']); 
     if ($res === TRUE) { 
      $zip->extractTo('../storage/unzipped/'); 
      $zip->close(); 

      // Get file, how? If I dont know the name, of the file inside the 
      // zip folder? 


     } else { 
      // Errors 
     } 
} 

我肯定知道如何打開該文件,如果我知道這個名字.....但在這種情況下,我不知道名字,所以我需要以某種方式得到它。 zip_entry_name()?

回答

0

好吧,我解決了之前任何人都可以回答...所以這裏是任何人需要它。

$entry = $zip->getNameIndex(0); 

或者,如果你的文件有多個文件夾/文件

$filenames=[]; 
for ($i = 0; $i < $zip->numFiles; $i++) { 
    $filenames[] = $zip->getNameIndex($i); 
}; 

的完整代碼

if($extension == 'ZIP' || $extension == 'zip'){ 
$zip = new ZipArchive; 
$res = $zip->open($_FILES['my_file']['tmp_name']); 
    if ($res === TRUE) { 
     $zip->extractTo('../storage/unzipped/'); 
     $filename = $zip->getNameIndex(0); 
     // Open file or stream it or whatever you want 
     // now that you have the name 
     $zip->close(); 
    } else { 
     // error 
    } 
} 
相關問題