PHP8 ZipArchive EM constant TRAD_PKWARE not working - php

i used ZipArchive::EM_TRAD_PKWARE, with password, but zip file is not encrypted. I think that pkware not working. Have solutions ?
Thx
$zip->setEncryptionName('test.txt', ZipArchive::EM_TRAD_PKWARE, 'PASSWORD');

I got it working by the following example and rules:
Two rules to make it work
I got inconsistent results when not following these 2 rules:
Make sure the file to be encrypted contains data
Make sure addFile is called before setEncryptionName
Working example
test.txt
Create the following test.txt file with the following content:
test
test.php
Add the following PHP code to test.php:
$zip = new ZipArchive();
$zip->open("test.zip", ZipArchive::CREATE);
$zip->addFile("test.txt");
$zip->setEncryptionName('test.txt', ZipArchive::EM_TRAD_PKWARE, 'PASSWORD');
$zip->close();
Run test.php
php test.php
A zip file is now created which prompts for a password on unzip.

Related

Get a file from a directory in laravel

I have been doing research on how to get a file out of directory using php/Laravel and nothing seems to be working.
I am running a command: app/commands/commandfile.php and I am trying to get a PNG file out of my directory: app/storage/resources/icon.png. Well I have tried several methods:
Storage::url('storage/resources/icon.png') and asset('storage/resources/')
But nothing seems to work. Can someone help me out?
If you are curious why I need this file. I am calling a notification system: php-fcm and trying to set the notification icon:
$note = new Notification('Catalyst', $message);
$note->setColor('#ffffff')
->setBadge(1)
->setIcon(
Thanks!

Php tmpfile only works from command line

When I run this:
$file = tmpfile();
$name = stream_get_meta_data($file)['uri'];
$nfile = new SplFileObject($name);
var_dump($nfile);
from the command line, it works as expected - creating a temporary file, and dumping details about it. However, when I run it through apache, $nfile is an empty object - it seems to be unable to access the file by path. Why is this? I have already confirmed that it is creating a file, and I can see the temporary file in the file system, so it's just php that can't reach it.

How do I make this script copy files to local disk?

I have copied and tried many PHP scripts from SO posts. I am trying to download files from a server running Centos. Via psftp (putty) I can login manually and copy files. But I want to automate the process, hence the need for a script.
On a similar server running on Windows am able to download files by ftp via a simple Perl script. On the Centos server I get connection refused with the Perl script. So I tried several php scripts. Are the scripts below (from SO posts) for the job? or what is wrong with the scripts?
script 1
#!/usr/bin/php
<?php
include('Net/SSH2.php');
$sftp = new Net_SFTP('xx.xx.xxx.xxx');
if (!$sftp->login('myuser', 'mypasswd')) {
exit('Login Failed');
}
// outputs the contents of filename.remote to the screen
echo $sftp->get('gateway_data*');
?>
Script 2
#!/usr/bin/php
<?php
include('Net/SSH2.php');
username='myuser';
password='mypasswd';
// Create SCP connection using a username and password
$scp = new SCP(
'xx.xx.xxx.xxx',
new SSH2Password($username, $password)
);
#################################
$sftp = ssh2_sftp($conn);
// Create a new local folder
ssh2_sftp_mkdir($sftp, './data');
// Retrieve a list of files
$files = scandir('ssh2.sftp://' . $sftp . '/data/gateway_data*');
################################################################
?>
In the first of PHP script you have posted you're doing echo $sftp->get('gateway_data*'); whereas in the Perl script you're doing cp gateway_data_301.txt. Try doing that in the PHP script. eg. echo $sftp->get('gateway_data_301.txt');.
As is it is unclear what you're expecting to happen. Unless the file name /actually/ has a wild card in it then are you expecting it to download every file that starts off with gateway_data* and just concatenate them in the output? Personally, I think just returning false or NULL would be better than that.
You can use your script 2 in PHP. However something is missing there. You are only opening the source directory. You must write a loop over all files in that folder.
// Retrieve a list of files
$files = scandir('ssh2.sftp://' . $sftp . '/data/gateway_data*');
foreach ($files as $key => $value) {
See the example of how to send a file with SFTP using SFTPConnection.

Creating a Password Protected Zip File using PHP in Windows

I am creating a zip file of a given file in PHP. Following is the function
function create_zip($file, $file_name)
{
$zip = new ZipArchive();
$zip_name = $file_name. ".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
if (file_exists($file)) {
$zip->addFromString(basename($file), file_get_contents($file));
} else {
return "file does not exist";
}
$zip->close();
return $zip_name;
}
I want to add password protection for the Zip files. I have found following code to create a password protected zip file
system('zip -P password file.zip file.txt');
But it is not working properly. Can you please Guide me How can I add the Password protection on the Zip file?
PHP Zip - http://php.net/manual/en/book.zip.php - doesn't support password protected zip files, you will need do it via command line.
To password protect a file, you need to use zip command line, make sure that the zip command line program is present, it is not installed by default on many systems.
In new PHP 5.6 (currently in beta) you can use your ZipArchive to create password protected archives. All you need is to add this code ZipArchive::setPassword($password)
Edit: Apparently it only supports decryption, not encryption yet.
I realize this is an old thread, but anyone struggling with adding a password to an archive in windows environment, I solved this using winrar command line and exec in PHP.
Download winrar from http://www.rarlab.com/ and include the WinRAR.exe in your PHP script directory, or call it from the right directory in your exec command.
exec("winrar a -p[password] [archive name] [file or folders to include]");
winrar in the above example refers to winrar.exe in the same directory as your script. If winrar.exe is NOT in the same directory as your script, you can simply include the path:
exec("C:\Program Files\Winrar...
So, for example:
exec("winrar a -ppassword archive.zip file.txt");
Will result in an archive named "archive.zip" being created with "file.txt" inside, and a password of "password".
For more information on winrar command line: http://acritum.com/software/manuals/winrar/

PHP ZipArchive what do status values mean?

I am right at the start of trying to write some PHP code to run on a Linux box on an EC2 server that will read files from my S3 bucket, zip them then write the zip file back to the bucket.
I have instantly run in to problems with even creating a simple zip archive from some images on the local disk of the EC2 instance, I am using a script to test out the idea from the PHP manual online and also have tried out a script from David Walsh - http://davidwalsh.name/create-zip-php which looks like it will be great. Neither result in an actual zip file and both give me different status results -
the first snippet from the php manual (to which i add the variable $thisdir)-
<?php
$zip = new ZipArchive();
$filename = "test112.zip";
$thisdir = "/uploads/";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n");
$zip->addFile($thisdir . "/too.php","/testfromfile.php");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
?>
output =
numfiles: 2 status:11
I dont see any zip file in my 'uploads' folder
The second bit of code i try ( i wont post the code here) - I pass real files and it returns
The zip archive contains 2 files with a status of 0
What are the status messages. I have checked to see if I have the correct libraries installed by looking at the output of phpinfo(); and under the ZIP heading I see -
Zip enabledExtension Version $Id: php_zip.c,v 1.1.2.43 2008/01/18 00:51:38 pajoye Exp $
Zip version 1.8.11
Libzip version 0.8.0-compatible
I have checked the permissions of the files PHP files with the code I am executing and they are set to 777 as is the folder I am trying to add the ziparchive to. I know that this should not remain at 777.
any ideas why I cannot see a zip file? what do the status values mean? Is there a good tutorial out there for using PHP to zip files on an Amazon S3 bucket? or a good utility to provide this functionality?
cheers
You can find it here:
http://www.php.net/manual/en/zip.constants.php
Error 11 is "Can't open file"
In my case i got error code 11 while opening a zip file for reading. The path and permissions are OK. I just changed the DIRECTORY SEPARATOR from back slash (\) to forward slash (/) and it worked. The strange thing is that forward slash (/) paths are in Linux where as I am working in windows server 2003.

Categories