简体   繁体   中英

How to create a password protected zip file on ftp server using php

I would like to create a password protected zip file on ftp server using php. I have try the code below but it does not work. The code below can work in local but when i put on ftp server it does not work. I have one client.php in local and one server.php in ftp server. I put the code below in server.php.(zipArchive or 7-zip both also can be accepted)

(The code below didn't include create password function. It just to create a zip file only.)

$zip = new ZipArchive;
if ($zip->open('Ftp://user.com/new/temp.zip', 
 ZipArchive::CREATE) === TRUE)
{
    // Add files to the zip file
    $zip->addFile('Ftp://user.com/new/temp/*');

    // All files are added, so close the zip file.
    $zip->close();
    echo"Create Successful";
}

Expected Output:

Password protected zip file create successful.

Actual Output:

No zip file created in ftp server.

<?php $target_dir = "uploads/"; $target_file = $target_dir . basename( $_FILES[ "fileToUpload" ][ "name" ] ); if ( isset( $_POST[ "submit" ] ) ) { if ( move_uploaded_file( $_FILES[ "fileToUpload" ][ "tmp_name" ], $target_file ) ) { echo "The file " . basename( $_FILES[ "fileToUpload" ][ "name" ] ) . " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } ?>

Here is the code which will create zip file only. I don't know how to achieve the zip encryption using ZipArchive() method

You may need to use setEncryptionName() method to achieve this

Or head over to the http://php.net/manual/en/ziparchive.setencryptionname.php

Try the following code

<?php 
$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();
<?php>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM