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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)