如何压缩图像文件并使用PHP将其解压缩?我的代码产生了错误的提取

如何压缩图像文件并使用PHP将其解压缩?我的代码产生了错误的提取,php,zipcode,ziparchive,Php,Zipcode,Ziparchive,我有一个表单,用户可以上传100MB的文件,这会导致上传延迟,因此我决定先在表单提交上压缩图像,然后上传到服务器上,然后在服务器上解压缩。因此,加载过程会减少,因此我使用了如下脚本: <?php $zip = new ZipArchive(); $filename = "newzip.zip"; if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { exit("cannot open <$filename>

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

<?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字节,并且已损坏,即无法查看

这个代码可能有什么问题,请指导我

$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

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

请检查下面的脚本,该脚本将仅从压缩文件中提取图像,并排除所有其他文件。类似于添加所有其他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();
}

那么我应该怎么做才能从这个代码中压缩图像呢?您的代码已经提取了压缩文件中的所有文件。。。只是您正在提取刚刚创建的zip文件,而该zip文件与表单相关的内容完全断开连接。请参阅wimvds答案。如何使用java小程序来实现这一点?