如何使用 PHP 压缩图像文件并提取它?我的代码产生损坏的提取

发布于 2024-09-08 11:22:25 字数 956 浏览 8 评论 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 字节并且已损坏,即它不能被已查看。

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

i have a form in which user can upload 100 mb file which results delay in upload, hence I decided to first zip the image on form submit and then upload it on server and then extract it on server. so that loading process decreases, Hence for this I have used a script which is as follows:

<?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");
}
?>

Now here from the above code I get the image extracted, but after extraction the size of image is reduced to 61 bytes and is corrupt i.e. it cannot be viewed.

What could be wrong with this code please guide me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静谧 2024-09-15 11:22:25

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

I think you are confusing client-side and server-side here. You simply can't create a ZIP clientside, since the PHP script is executed on the server side. So you either have to instruct your users to zip the files before submitting them, or use ie. a Java applet to zip the file for them before uploading.

你是我的挚爱i 2024-09-15 11:22:25
$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->addFromString("myfile.jpeg", 
"This is the first file in our ZIP, added as 
firstfile.txt.\n");

Perhaps you wanted instead:

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

The 61-byte file you're getting is the one you added in the first place!

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

gives 61.

洋洋洒洒 2024-09-15 11:22:25

请检查下面的脚本,该脚本只会从 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();
}

Please check below script which will only extract images from zip file and exclude all other files. Like ways you can add all other mime type.

<?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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文