PHP创建zip存档添加本地磁盘

PHP创建zip存档添加本地磁盘,php,zip,Php,Zip,我想在一个zip中导出我的所有图像,但出于某种原因,它也添加了我的所有本地磁盘。。。我不明白 代码如下: $files = $urls; $zipname = 'uploads.zip'; $zip = new ZipArchive; $zip->open($zipname, ZipArchive::CREATE); foreach ($files as $file) { $name = explode( '/', $file); $zip->addFile($fi

我想在一个zip中导出我的所有图像,但出于某种原因,它也添加了我的所有本地磁盘。。。我不明白

代码如下:

$files = $urls;
$zipname = 'uploads.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);

foreach ($files as $file) {
    $name = explode( '/', $file);
    $zip->addFile($file, pathinfo( $file, PATHINFO_BASENAME ));
}

$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));

readfile($zipname);
$files
是图像位置的数组:

ServicesController.php on line 61:
array:3 [▼
  0 => "C:\wamp\www\prjct\app/../../prjct/web/uploads/media/default/0001/15/thumb_14794_default_big.gif"
  1 => "C:\wamp\www\prjct\app/../../prjct/web/uploads/media/default/0001/15/thumb_14794_default_small.gif"
  2 => "C:\wamp\www\prjct\app/../../prjct/web/uploads/media/default/0001/15/thumb_14794_admin.gif"
]
当我选择我的zip文件时,我看到:

正如您所看到的,我的本地磁盘交换机不应该在这里。

这应该可以工作:

$files = $urls;
$zipname = 'uploads.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);

$rootPath = realpath($files);
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath),
    RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file) {
    // Get real and relative path for current file
    $filePath = $file->getRealPath();
    $relativePath = substr($filePath, strlen($rootPath) + 1);

    // non-empty directories would be added automatically
    if (!$file->isDir()){
        // Add current file to archive
        $zip->addFile($filePath, $relativePath);    
    }
}

$zip->close();
通常,应该使用目录分隔符预定义常量,而不是斜杠/反斜杠。这在Windows计算机上开发时特别有用

通常,在Windows上工作时,在向zip添加文件夹时,请确保删除尾随斜杠-这就是在zip文件中创建本地磁盘的原因

$localDirNoSlashes = rtrim($localDir, DIRECTORY_SEPARATOR);
$zip->addEmptyDir($localDirNoSlashes);
这让我疯狂了一段时间,直到我意识到发生了什么…

这应该是可行的:

$files = $urls;
$zipname = 'uploads.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);

$rootPath = realpath($files);
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath),
    RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file) {
    // Get real and relative path for current file
    $filePath = $file->getRealPath();
    $relativePath = substr($filePath, strlen($rootPath) + 1);

    // non-empty directories would be added automatically
    if (!$file->isDir()){
        // Add current file to archive
        $zip->addFile($filePath, $relativePath);    
    }
}

$zip->close();
通常,应该使用目录分隔符预定义常量,而不是斜杠/反斜杠。这在Windows计算机上开发时特别有用

通常,在Windows上工作时,在向zip添加文件夹时,请确保删除尾随斜杠-这就是在zip文件中创建本地磁盘的原因

$localDirNoSlashes = rtrim($localDir, DIRECTORY_SEPARATOR);
$zip->addEmptyDir($localDirNoSlashes);

这让我疯狂了一段时间,直到我意识到发生了什么…

在$URL变量中是什么?你能打印它吗?你能上传创建的zip文件吗?它的大小是多少?在$URL变量中是什么?你能打印它吗?你能上传创建的zip文件吗?多大尺寸?