簡體   English   中英

如何在 php 中檢查 zip 文件

[英]How to check zip file or not in php

我正在 PHP 中創建 zip 和 rar 文件發布腳本。 我想在上傳文件 zip 或 rar 我的腳本不檢查任何文件之前檢查。 當我添加 zip 但每次都顯示此消息時this is not a zip file

這是我的代碼

if(!empty($_FILES['zip']['tmp_name'][0])){
$zipType = array('application/zip');
$zip = $_FILES['zip']['tmp_name'][0];
if(!in_array($zip, $zipType)){
die("this is not a zip file");
}
}
if(is_resource($zip = zip_open($filename))
{
    zip_close($zip);
    //this is a zip archive
}
else(($rar = RarArchive::open($filename)) !== FALSE)
{
    $rar->close();
    //this is a rar archive
}
else
{
    //this is not a zip or rar archive
}

使用 php 檢查文件是否為存檔(zip 或 rar)

使用mime-content-type

$type = mime_content_type($filename);

但是,如果這些是正在上傳的文件,那么這是浪費的,因為您需要等待用戶上傳文件才能檢查它。 您應該使用 html 中的文件閱讀器 api 來檢查文件,然后再上傳。 您可以在瀏覽器中檢查文件類型、魔術字節、md5 hash 等,以在上傳前后接受/拒絕/驗證文件

您可以找到上傳文件的類型為

if(!empty($_FILES['zip']['tmp_name'])){
$zip = $_FILES['zip']['type'];
if($zip !== 'zip')){
die("this is not a zip file");
}
}

並檢查您的文件大小並將其限制為 10mb

if(!empty($_FILES['zip']['tmp_name'])){
$zip = $_FILES['zip']['type'];
$size = $_FILES['zip']['size'];
if($zip !== 'zip')){
die("this is not a zip file");
}
if($size > 10000000){
echo 'File is too large';
 }
}

而且我認為沒有必要在這里創建一個數組。

暫無
暫無

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

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