dousunle5801 2017-03-30 05:54
浏览 130
已采纳

PHP - 在php中创建zip后,是否可以将zip文件上传到数据库?

So, I watched a tutorial from a youtube video here how to zip a file using php. This is the code I copied.

This is the form to upload zip. I choose 7 files and then name the archive name "testing".

zip file

After I clicked the "Add Files to Zip", the files are zipped and are sent to the directory.

zip file 2

These are the code

HTML Code:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Zip Archiving Tutorial</title>
</head>

<body>

<form action="" method="POST" enctype="multipart/form-data">
Archive Name: <input type="text" name="name"><br />
<input type="file" name="files[]" multiple="true">
<input type="submit" name="create" value="Create Zip"><br><br>
<input type="submit" name="upload" value="Upload to Database">

</form>

PHP Code:

<?php

if(isset($_POST['create']))
{
$filesArray = $_FILES["files"];

for($num = 0; $num < count($filesArray["name"]); $num++)
{
    $fileName = $filesArray["name"][$num];
    $tempName = $filesArray["tmp_name"][$num];

    move_uploaded_file($tempName,"tmp/".$fileName);
}

$archiveName = $_POST['name'].".zip";
$filesArrayNames = $_FILES["files"]["name"];

$zipsDir = scandir("zips/");
$error = false;
foreach($zipsDir as $zipDirFile)
{
    if($zipDirFile == $archiveName)
    {
        $error = true;
        break;
    }
}

if($error == false)
{
    $tmpDir = scandir("tmp/");
    $zip = new ZipArchive;
    $zip->open("zips/".$archiveName, ZipArchive::CREATE);

    for($num = 0; $num < count($filesArray["name"]); $num++)
    {
        $fileName = $filesArray["name"][$num];
        foreach($tmpDir as $tmpDirFile)
        {
            if($tmpDirFile == $fileName)
            {
                $zip->addFile("tmp/".$fileName);
                echo "Adding: ".$fileName."<br />";
            }
        }
    }
    $zip->close();

    for($num = 0; $num < count($filesArray["name"]); $num++)
    {
        $fileName = $filesArray["name"][$num];
        foreach($tmpDir as $tmpDirFile)
        {
            if($tmpDirFile == $fileName)
            {
                unlink("tmp/".$fileName);
            }
        }
    }
}


else
{
    echo "Name already exists";
}
}

if (isset($_POST['upload']))
{
    //code to add for uploading zip file to database

}

?>

But is it possible to upload the zip file after it is the files is zipped through php? I want the user to upload the files after the files are zipped.

  • 写回答

1条回答 默认 最新

  • dtp791357 2017-03-30 06:18
    关注

    PHP does not work on the user machine(the client) but in the server, so if you want to zip and then upload files think of scripts that run in the client side like javascript(angularjs, jquery and other libraries you can use) to use to zip you file and php to accept them into the server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分