2017-07-05 135 views
0

我在這裏變得非常困惑。所以這裏是我的項目發生了什麼。用戶點擊下載然後選擇目錄來保存文件,該文件將是一個zip文件。之後,我想在用戶選擇的相同目錄中提取該zip文件。這甚至可能在一個腳本中?Zip文件 - 下載 - 然後解壓

這裏是我的PHP代碼

<?php 
require_once('connect.php'); 
// Get real path for our folder 
$rootPath = realpath($_GET['uniq']); 

// Name of the zip derive from the database 
$zipname = $_GET['name'] . '.zip'; 
// Initialize archive object 
$zip = new ZipArchive(); 
$zip->open($zipname, ZipArchive::CREATE | ZipArchive::OVERWRITE); 

// Create recursive directory iterator 
/** @var SplFileInfo[] $files */ 
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath), 
    RecursiveIteratorIterator::LEAVES_ONLY 
); 

foreach ($files as $name => $file) 
{ 
    // Skip directories (they would be added automatically) 
    if (!$file->isDir()) 
    { 
     // Get real and relative path for current file 
     $filePath = $file->getRealPath(); 
     $relativePath = substr($filePath, strlen($rootPath) + 1); 

     // Add current file to archive 
     $zip->addFile($filePath, $relativePath); 
    } 
} 

// Zip archive will be created only after closing object 
$zip->close(); 
header('Content-Type: application/zip'); 
header('Content-disposition: attachment; filename='.$zipname); 
header('Content-Length: ' . filesize($zipname)); 
readfile($zipname); 
?> 

如果它不可能另外一個問題。是否有可能讓用戶選擇一個zip文件,然後在某些客戶端目錄中提取它,即。 C://xamp/extracthere/使用javascript。

+1

您不能強制客戶端解壓縮有效負載。一旦他們下載了有效載荷,這取決於他們對它做了什麼。想想看,你甚至不知道客戶端是否安裝瞭解壓縮程序,更不用說它是什麼了(命令行工具,WinZip等),並且只是想到可能確保我包含惡意軟件的大屠殺壓縮文件中的有效載荷。 – GordonM

+0

@GordonM我實際上正在創建一個用戶無法訪問驅動器的軟件。我需要在軟件中完成這一切。我明白你想說什麼。所以我遇到了另一個解決方案,用戶將不得不選擇他/她想要提取的zip文件。迄今爲止它對我成功。謝謝 –

+0

在私有文件和用戶之間添加網守層非常好,這是很常見的做法,但仍然無法強制客戶端解壓縮有效負載。瀏覽器是故意設計的。 – GordonM

回答

1

一旦用戶將文件下載到他們的機器,它就無法控制。你不能強迫他們解壓縮它,或者爲此做任何事情。

想象一下,如果你可以控制用戶本地磁盤上的文件,這將是黑客的夢想。在每種情況下,出於類似的原因,使用PHP和JavaScript都是不可能的。