I am trying to extract a ZipArchive to 'http://localhost/MODULES/ZIP_RAR_MANAGER/'.
No errors are shown, and I get the message Zip File Opened.
Why are the files not being correctly extracted from the zip archive?
$zip = new ZipArchive;
if ($zip->open('../test.zip'))
{
echo 'ZIP FILE OPENED...<br/>';
if ($zip->extractTo('http://localhost/MODULES/ZIP_RAR_MANAGER/'))
{
echo 'ZIP FILE EXTRACTED';
}
$zip->close();
}
else
{
echo 'failed';
}
You are trying to extract it to non-existent folder http://localhost/MODULES/ZIP_RAR_MANAGER/. You cannot use url as a folder/file path.
You should use:
if ($zip->extractTo($_SERVER['DOCUMENT_ROOT'] . '/MODULES/ZIP_RAR_MANAGER/') {
...
}
Related
I'm running a php script (like bash).
This script do this:
Download a zip file from FTP server
ftp_get($ftp, $myFile.zip, $file, FTP_BINARY);
When I have downloaded the file, I want to extract its content:
$zip = new ZipArchive;
$res = $zip->open($myFile);
if ($res === TRUE) {
$zip->extractTo($extractDir);
$zip->close();
} else {
dump( 'error, code:' . $res);
die;
}
But, I get the error 19 who is: "Not a zip archive"
If I open the .zip, it is a compressed archive because I have the PK key...
Does anyone have a solution or idea ?
Regards
Solutions
I am using this code and it work fine.
$zip = new ZipArchive;
$myfileDir = './abc.zip';
$extractDir = './';
$res = $zip->open($myfileDir);
if ($res === TRUE) {
$zip->extractTo($extractDir);
$zip->close();
echo 'Successfully extract the file';
} else {
echo ('error, code:' . $res);
die;
}
Question
I believe the problem is because of the zip file, would upload the the zip folder to google drive and let us download and test it locally?
I'm trying to extract a directory of zip files in the same directory they are located in.
public function buildRecords($quickCheckOutputDir, $salvageVinsDir)
{
//unzip
$files_to_extract = $this->getFiles($salvageVinsDir);
$zip = new ZipArchive();
foreach($files_to_extract as $file) {
$res = $zip->open($salvageVinsDir . $file);
if($res) {
echo $zip->extractTo($salvageVinsDir);
//$zip->deleteName($salvageVinsDir . $file);
$zip->close();
} else {
echo 'failed, code:' . $res;
}
}
}
The echo $zip->extractTo($salvageVinsDir) returns 1 so I would think that the method is working but when I check the directory only the zipped files are there. Nothing has been extracted. What is the issue here?
Edit: I gave the directory I'm working in chmod 777.I'm working on centos 7.
I am using php to open a zip file and read its content and then copy this content to some other location. I am stuck at one point that how to copy the file content(file is a json) and also have some images in other directories which I want to access.
$zip = zip_open(Input::file('file'));
$rzip = zip_read($zip);
$entr_open_zip = zip_entry_open($zip, $rzip, '');
if ($entr_open_zip) {
return "hello";
die;
} else {
return "hi";
die;
}
I have opened the file but don't know how to copy content or even how to open the directory and access files in that directory. Any help is appreciated. I will explain more if needed.Ignore the typos.
I have seen some questions related to it but not getting satisfactory solution.
ZipArchive in PHP have functions to handle .zip files. In your case to extract all contents to specific directory, extractTo function can make help.
Sample Code:
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->extractTo('/my/destination/dir/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
So, I creating a zip file with a password:
function createZip($fileName,$fileText,$zipFileName,$zipPassword)
{
shell_exec('zip -P '.$zipPassword.' '.$zipFileName.'.zip '.$fileName);
unlink($fileName);
return file_exists($zipFileName.'.zip');
}
$filex = "/backup/home/fyewhzjp/long_location_of_a_file/temp/data/map10/data.txt";
// $file_content = 'test';
$archive = "/backup/home/fyewhzjp/long_location_of_a_file/temp/data/map10/archive";
createZip($filex,$file_content,$archive,$pass);
And it works. I'm getting a archive.zip in my /temp/data/map folder on the website. But, when I open my archive I can see a bunch of folders, and data.txt at the end, let's say it will be
/backup/home/fyewhzjp/long_location_of_a_file/temp/data/map10/data.txt
So, I need to leave only data.txt in my folder, without other folders. How can I do it?
If anyone will face the same problem as I did, here is the solution:
Just add -jrq after zip in shell_exec like this:
shell_exec('zip -jrq -P '.$zipPassword.' '.$zipFileName.'.zip '.$fileName);
After that, full path will be ignored.
In addition to #Script47...
Pure PHP Available as of PHP 7.2.0 and PECL zip 1.14.0, respectively, if built against libzip ≥ 1.2.0.
<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
// Add files
$zip->addFromString('test.txt', 'file content goes here');
$zip->addFile('data.txt', 'entryname.txt');
// Set global (for each file) password
$zip->setPassword('your_password_here');
// This part will set that 'data.txt' will be encrypted with your password
$zip->setEncryptionName('data.txt', ZipArchive::EM_AES_128); // Have to encrypt each file in zip
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
Rather than using shell_exec why don't you just use the ZipArchive class with the functions ZipArchiveOpen::open and ZipArchive::setPassword, it seems that that would make things a lot easier.
<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFromString('test.txt', 'file content goes here');
$zip->addFile('data.txt', 'entryname.txt');
$zip->setPassword('your_password_here');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
Note:
This function only sets the password to be used to decompress the archive; it does not turn a non-password-protected ZipArchive into a password-protected ZipArchive.
I am trying to delete a file from folder in php here is my model function
function deleteFiles()
{
$file = "http://localhost/copycopy/img/uploaded/long.jpeg";
if(is_file($file))
{
#unlink($file); // delete file
echo $file.'file deleted';
}
else
{
echo "no file";
}
}
but I always see "no file" and file is never deleted, file is in folder,because the url in $file actually displays the file in browser
help me
instead of using web url
$file = "http://localhost/copycopy/img/uploaded/long.jpeg";
use local path to file:
$file = $pathToYourWebSite . "/copycopy/img/uploaded/long.jpeg";
be sure to set $pathToYourWebSite to real location of your website;