Zip Not Being Created - php

I am trying to create a page that simply zips files, that's all there is to it. I added the check to make sure the method was actually being called, and it is, I am seeing the "ok" when the code is run, however, no ZIP file is being created.
Any help would be appreciated. I can confirm that those files exist. (They are in relation to where the php file is)
$zip = new ZipArchive;
if ($zip->open('/files/custom.zip', ZipArchive::CREATE) === TRUE) {
$zip->addFile('/files/textures/16/default/pack.mcmeta');
$zip->addFile('/files/textures/16/default/pack.png');
$zip->close();
echo "ok";
} else {
echo "failed";
}

try this
<?php
$zip = new ZipArchive;
if ($zip->open('/files/custom.zip') === TRUE) {
$zip->addFile('/files/textures/16/default/pack.mcmeta', 'pack.png');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>

Related

Recurse through a directory with php and add a file to many zips

This will be simple for someone, so hope you don't mind my asking here, I have some php code like so:
<?php
$zip = new ZipArchive;
if ($zip->open('myfiles1.zip) === TRUE) {
$zip->addFile('readme.txt', 'readme.txt');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
I would like to make it recurse through the directory it is in and add the same file "readme.txt" to all the zips inside it.

extract Dropbox Zip on server using php

I'm building a website for my band. However, as I don't want to be the only one maintaining the site, I want to allow the other members to contribute via a dropbox-folder.
I have received a direct download link from dropbox that zip's the whole folder.
this links looks something like this:
http://www.dropbox.com/sh/xxxxxxxxxxxxxxx/yyyyyyyyyyyyyyyyyyyyyyyyy?dl=1
A folder test with 777 rights is available on the server side.
However the dropbox zip-file won't extract, and windows-created zip-file does.
<?php
echo PHP_OS.' PHP:'.phpversion();
echo '<br>starting download<br>';
$dbLink = 'http://www.dropbox.com/sh/xxxxxxxxxxxxxxx/yyyyyyyyyyyyyyyyyyyyyyyyy?dl=1'
$success = file_put_contents("./test.zip", fopen($dbLink, 'r'));
if ($success === FALSE) {
echo 'error storing zip';
} else {
echo 'success storing zip';
}
echo '<br>';
$zip = new ZipArchive;
$result = $zip->open('./test.zip');
if ($result === TRUE) {
echo 'opened zip<br>';
$success = $zip->extractTo('./test/');
if ($success === TRUE) {
echo 'unzip complete';
} else {
echo 'unzip failed';
}
$zip->close();
} else {
echo 'unable to open zip';
}
?>
I'd like to hear your thought on this.

php: Is it possible to extract a zip file from a URL without writing the zip to disk?

I'm grabbing the zip file from a URL, then I copy the zip onto disk only to grab it from disk and extract it. I want to eliminate the step of having to write the zip to disk to optimize it. Here is my code...
$url_zip = 'http://returns_my_zip_file';
$new_file = '/zip_name.zip';
if (!copy($url_zip, $new_file)) { //<--- I want to eliminate this step.
echo "\n\nFailed to copy file...\n\n";
}
$zip = new ZipArchive;
if(is_readable($new_file)) {
$zip = new ZipArchive;
if ($zip->open($new_file) === TRUE) {
$zip->extractTo('ExtractToThisFolder');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
}
Thanks in advance.

Get ZIP file from generated URL

I have a URL that, when requested, start the download of a .zip file. The URL looks something like this:
http://website.nl/servlets/ogexport?koppeling=WEBSITE&user=&password=&og=BOG&kantoor=**
How can I make sure that the zip file that gets generated in that proces get used in my PHP unzip script?
$file = 'testing.zip';
$path = pathinfo(realpath($file), PATHINFO_DIRNAME).'/xml';
// unzippen
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
echo 'success';
// debug("ZIP bestand uitgepakt", 2);
} else {
// debug("ZIP niet uitgepakt", 2);
echo 'something went wrong.';
}
$xml_path = $path."/testing.xml";
echo $file.'<br>';
echo $xml_path;
Hoping for help!
I would recommend using php curl to download the remote file.Here is an example

No error when creating zip, but it doesn't get created

I wrote this code to create a ZIP file and to save it. But somehow it just doesn't show any error, but it doesn't create a ZIP file either. Here's the code:
$zip = new ZipArchive;
$time = microtime(true);
$res = $zip->open("maps/zips/test_" . $time . ".zip", ZipArchive::CREATE);
if ($res === TRUE) {
echo "RESULT TRUE...";
$zip->addFile("maps/filename.ogz","filename.ogz"); //Sauerbraten map format
$zip->addFromString('how_to_install.txt', 'Some Explanation...');
$zip->close();
$zip_created = true;
echo "FILE ADDED!";
}
What am I doing wrong, and how can I fix it?
Probably apache or php has not got permissions to create zip archives in that directory. From one of the comments on ZipArchice::open:
If the directory you are writing or
saving into does not have the correct
permissions set, you won't get any
error messages and it will look like
everything worked fine... except it
won't have changed!
Instead make sure you collect the
return value of ZipArchive::close().
If it is false... it didn't work.
Add an else clause to your if statement and dump $res to see the results:
if($res === TRUE) {
...
} else {
var_dump($res);
}
There are 2 cases when zip doesn't generate the error.
Make sure every file you are adding to the zip is valid. Even if
one file is not available when zip->close is called then the archive
will fail and your zip file won't be created.
If your folder doesn't
have write permissions zip will not report the error. It will finish
but nothing will be created.
I had an exactly same issue, even when with full writing/reading permissions.
Solved by creating the ".zip" file manually before passing it to ZipArchive:
$zip = new ZipArchive;
$time = microtime(true);
$path = "maps/zips/test_" . $time . ".zip"
touch($path); //<--- this line creates the file
$res = $zip->open($path, ZipArchive::CREATE);
if ($res === TRUE) {
echo "RESULT TRUE...";
$zip->addFile("maps/filename.ogz","filename.ogz"); //Sauerbraten map format
$zip->addFromString('how_to_install.txt', 'Some Explanation...');
$zip->close();
$zip_created = true;
echo "FILE ADDED!";
}
Check out that each of your file exists before calling $zip->addFile otherwise the zip won't be generated and no error message will be displayed.
if(file_exists($fichier->url))
{
if($zip->addFile($fichier->url,$fichier->nom))
{
$erreur_ouverture = false;
}
else
{
$erreur_ouverture = true;
echo 'Open error : '.$fichier->url;
}
}
else
{
echo 'File '.$fichier->url.' not found';
}
break it into steps.
if ($res === TRUE) {
check if file_exist
check if addFile give any error
}
if($zip->close())
{
$zip_created = true;
echo "FILE ADDED!"
}
Check the phpinfo for zip is enabled or not :)
One of the reasons for zip file is not created is due to missing check if you are adding file and not a directory.
if (!$file->isDir())
I found the solution here.

Categories