【问题标题】:How to upload files as zip folder and set password in laravel 5.4如何将文件上传为 zip 文件夹并在 laravel 5.4 中设置密码
【发布时间】:2018-02-18 17:08:37
【问题描述】:

作为我的网络应用程序的一部分,我想上传文件。
我想用密码保护他的文件。
我要做的是获取上传的文件,压缩它,然后给 .zip 文件一个密码。
我知道如何在 laravel 中上传文件,但我不知道如何压缩和设置密码。
我正在使用以下代码上传文件。

$mobimg=$request->file('img'); 
$newfile2 = rand(456,987).time().$mobimg->getClientOriginalName();
 $mobimg->move('images/upload', $newfile2);

 $data=array(                      
               'img' => $newfile2,

               );
               DB::table('file_upload')->insert($data);

我希望有人能帮我做到这一点
提前致谢。

【问题讨论】:

标签: php laravel file-upload zip


【解决方案1】:

您至少需要 PHP 7.2 才能使用密码加密 ZIP 文件。

$zip = new ZipArchive();

$zipFile = __DIR__ . '/output.zip';
if (file_exists($zipFile)) {
    unlink($zipFile);
}

$zipStatus = $zip->open($zipFile, ZipArchive::CREATE);
if ($zipStatus !== true) {
    throw new RuntimeException(sprintf('Failed to create zip archive. (Status code: %s)', $zipStatus));
}

$password = 'top-secret';
if (!$zip->setPassword($password)) {
    throw new RuntimeException('Set password failed');
}

// compress file
$fileName = __DIR__ . '/test.pdf';
$baseName = basename($fileName);
if (!$zip->addFile($fileName, $baseName)) {
    throw new RuntimeException(sprintf('Add file failed: %s', $fileName));
}

// encrypt the file with AES-256
if (!$zip->setEncryptionName($baseName, ZipArchive::EM_AES_256)) {
     throw new RuntimeException(sprintf('Set encryption failed: %s', $baseName));
}

$zip->close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 2018-02-04
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    相关资源
    最近更新 更多