0

我有一个表单,用户可以在其中上传 100 mb 文件,这会导致上传延迟,因此我决定先在表单提交时压缩图像,然后将其上传到服务器上,然后在服务器上提取它。以便加载过程减少,因此为此我使用了如下脚本:

<?php
$zip = new ZipArchive();
$filename = "newzip.zip";

if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
   exit("cannot open <$filename>\n");
}
$zip->addFromString("myfile.jpeg", 
"This is the first file in our ZIP, added as 
firstfile.txt.\n");

echo "numfiles: " . $zip->numFiles . "\n";
$zip->close();

$zip1 = zip_open("newzip.zip");
if ($zip1) {
  while ($zip_entry = zip_read($zip1)) {
    $fp = fopen(zip_entry_name($zip_entry), "w");
    if (zip_entry_open($zip1, $zip_entry, "r")) {
      $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
      fwrite($fp,"$buf");
      zip_entry_close($zip_entry);
      fclose($fp);
    }
  }
  zip_close($zip1);
  unlink("newzip.zip");
}
?>

现在,从上面的代码中,我得到了提取的图像,但提取后图像的大小减少到 61 个字节并且已损坏,即无法查看。

这段代码有什么问题请指导我

4

3 回答 3

2

我认为您在这里混淆了客户端和服务器端。您根本无法创建 ZIP 客户端,因为 PHP 脚本是在服务器端执行的。因此,您要么必须指示用户在提交文件之前压缩文件,要么使用 ie. 一个 Java 小程序,用于在上传之前为他们压缩文件。

于 2010-07-08T09:55:54.840 回答
0
$zip->addFromString("myfile.jpeg", 
"This is the first file in our ZIP, added as 
firstfile.txt.\n");

也许您想要:

$zip->addFromString("firstfile.txt", 
"This is the first file in our ZIP, added as 
firstfile.txt.\n");

您获得的 61 字节文件是您最初添加的文件!

echo strlen("This is the first file in our ZIP, added as 
firstfile.txt.\n");

61.

于 2010-07-08T09:41:36.700 回答
0

请检查下面的脚本,它只会从 zip 文件中提取图像并排除所有其他文件。就像您可以添加所有其他 mime 类型一样。

<?php
$dir = __DIR__;
$extractedFilesDir = $dir . "/images";
$zipFile = $dir . "/image.zip");

$zipFileName = basename($zipFile, '.zip');
$fieDir = $extractedFilesDir . '/' . $zipFileName;
if (class_exists('ZipArchive')) {
    $zip = new ZipArchive;
    $result = $zip->open($zipFile, ZipArchive::CHECKCONS);
    if ($result === TRUE) {
        $noImageFound = true;
        for ($i = 0; $i < $zip->numFiles; $i++) {
            $onlyFileName = $zip->getNameIndex($i);
            $fileType = mime_content_type('zip://' . $zipFile . '#' . $onlyFileName);
            $fileType = strtolower($fileType);
            if (($fileType == 'image/png' || $fileType == 'image/jpeg' || $fileType == 'image/gif' || $fileType == 'image/svg') && (preg_match('#\.(SVG|svg|jpg|jpeg|JPEG|JPG|gif|GIF|png|PNG)$#i', $onlyFileName))) {
                //copy('zip://' . $zipFile . '#' . $onlyFileName, $fieDir . '/' . $onlyFileName);
                $zip->extractTo($extractedFilesDir, array($zip->getNameIndex($i)));
                $noImageFound = false;
                echo 'extracted the image ' . $onlyFileName . ' from ' . $zipFile . ' to ' . $fieDir . '<br />';
            }
        }
        if ($noImageFound) {
            echo 'There is no images in zip file ' . $zipFile . '<br />';
        }
    } else {
        switch ($result) {
            case ZipArchive::ER_NOZIP:
                echo 'Not a zip archive ' . basename($zipFile);
            case ZipArchive::ER_INCONS:
                echo 'Consistency check failed ' . basename($zipFile);
            case ZipArchive::ER_CRC:
                echo 'checksum failed ' . basename($zipFile);
            default:
                echo 'Error occured while extracting file ' . basename($zipFile);
        }
    }
    $zip->close();
}
于 2017-08-08T05:43:26.510 回答