繁体   English   中英

压缩文件-下载-然后解压缩

[英]Zip File - Download - Then Unzip

我在这里真的很困惑。 所以这是我的项目中发生的事情。 用户单击下载,然后选择目录以保存文件,该文件将是一个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。

一旦用户将文件下载到他们的计算机上,那就超出了您的控制范围。 您不能强迫他们解压缩该文件,也不能对此做任何其他事情。

想象一下,如果您可以控制用户本地磁盘上的文件,那将是黑客的梦想。 出于各种原因,使用PHP和JavaScript都是不可能的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM