簡體   English   中英

使用Php檢查zip或rar存檔是空的還是已損壞

[英]Check if zip or rar archive is empty or corrupted with Php

我有一個非常簡單的腳本,允許用戶只上傳.zip或.rar文件。 我想知道如何知道文件是否已損壞或為空?

這是我的劇本

if(isset($_POST['customerid']) && $_FILES['file']['tmp_name'] && $_POST['requestid']){

            //post variables
            $customerid = $_POST['customerid'];
            $filename = $_FILES['file']['name'];
            $requestid = $_POST['requestid'];

            //check if the file is .rar or .zip
            $fileInfo = new finfo(FILEINFO_MIME_TYPE);
            $fileMime = $fileInfo->file($_FILES['file']['tmp_name']);
            $validMimes = array( 
              'zip' => 'application/zip',
              'rar' => 'application/x-rar',
            );

            $fileExt = array_search($fileMime, $validMimes, true);
            if($fileExt != 'zip' && $fileExt != 'rar'){

                echo 'Not a zip or rar.';
            }

            //check if the file is corrupted or empty


            //if all OK insert file name and path to database
            $uservalida_stmt = $conn->prepare("INSERT INTO user_project_files (dateCreated,userid,projectFile,serviceRequestId) VALUES (?,?,?,?)");
            $uservalida_stmt ->bind_param('siss',$currentdate,$customerid,$filename,$requestid);
            $uservalida_stmt ->execute();
            $uservalida_stmt ->close();

            //move upload and EXTRACT file to directory
            move_uploaded_file($_FILES['file']['tmp_name'], '../user/project/' . $_FILES['file']['name']);

        }

PHP手冊有zip的例子。 http://php.net/manual/en/zip.examples.php

<?php
$za = new ZipArchive();

$za->open('test_with_comment.zip');
print_r($za);
var_dump($za);
echo "numFiles: " . $za->numFiles . "\n";
echo "status: " . $za->status  . "\n";
echo "statusSys: " . $za->statusSys . "\n";
echo "filename: " . $za->filename . "\n";
echo "comment: " . $za->comment . "\n";

echo "numFile:" . $za->numFiles . "\n";
?>

或者只是在open函數上查看錯誤代碼( http://php.net/manual/en/ziparchive.open.php


您也可以使用RAR文件( http://php.net/manual/en/rararchive.open.php )進行類似操作,但需要先安裝它( http://php.net/manual/en/rar.installation .php )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM