PHP zip from file - php

Attempting to create a kmz file from a kml file. The script was working last week, but somehow it stopped working. I am attempting to troubleshoot this issue by creating a .zip file which contains a .kml file.
The process is as follows
1. Kml file is created
2. .zip file is added
3. kml file is added to zip according to name
4. .zip is saved
The issue is that when I open the directory the .zip contains that looks like the file structure of the .kml file, not just the file.
For example if the .kml was inside c:/foldera/folderb. The zip would contain c:/foldera/folderb/kml.
$zip = new ZipArchive();
$zip_name = $_SERVER['DOCUMENT_ROOT'] .'/assets/kml'. "/r".$r.".zip";
$filename = $_SERVER['DOCUMENT_ROOT'] .'/assets/kml'. "/r".$r.".kml";
$zip->open($zip_name, ZIPARCHIVE::CREATE);
$zip->addFile($filename);
$zip->close();
unlink($_SERVER['DOCUMENT_ROOT'] .'/assets/kml'. "/r".$r.".kml");

For some reason you have two addfile's:
$zip->addFile($filename, ltrim($filename, '/'));
$zip->addFile($filename);
Try replacing those two lines with this:
$zip->addFile($filename, basename($filename));
basename() returns only the filename, removing the path - http://www.php.net/manual/en/function.basename.php

Related

php upload a file into current directory

I want to upload an mp3 file and add it into a directory on my server. I am executing the php code from the server directory I want to add the file to. This is what I have so far:
$dir=basename(dirname(__FILE__));
$folder = dirname(dirname($_SERVER['SCRIPT_NAME']));
$file_name = $_FILES['upload']['name'];
//remember to remove this line
$file_name = "test.mp3";
$add_file[0] = "<a
href='//mydomain.com$folder/$dir/$file_name'>$file_name</a>";
$path = "https://nerjabible.com$folder/$dir/";
so I have $add_file[0] and I have the $path to append to. I have tried put and write append, but they all seem to work by opening a file, I am already in the directory, I just want to append my linked file to the existing links in the directory.
The mp3 is in a directory on my PC as a hyperlink playable file and I am using a file upload script to obtain the file which ends up in $file_name but as a non linked file name, so i want to append this into the same directory the script is running from, but as a linked file.
printscreen of the local and remote directories

Strange characters after extension creating zip file in php - IIS

After moving a website from one server to another, both running IIS, when a zip file creation script is called the file created has some random characters at the end, ex. what should be 'test.zip' is converted to 'test.zip08c1b35d' giving error when trying to download them.
I've discovered that it only happens inside the website path, if I create a symbolic link inside the website path with the same name, pointing to a folder outside the website path the zip file is created with the correct name. Any other folder within the website path the name is changed.
The zip file format is correct as if I delete the characters and leave the name ending '.zip' the file opens fine.
The code used is:
$dir ='../mylims/test/';
$destination = $dir.'test.zip';
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
echo "Error";
}
$zip->addFile('./test/testfile.txt','testfile.txt');
$zip->close();
Any suggestions what to look? Tks

override all files and folders while unziping except given folder name using php

My file structure in zip is
lib
css
js
app1
app2
index.php
All the above files and folders are in test.zip
i want to extract this test.zip using php
$zip = new ZipArchive;
$res = $zip->open("test.zip");
if ($res === TRUE)
{
$zip->extractTo('path');
$zip->close();
}
if lib folder is already exist in the directory where we are unzipping then except lib(folder) all other files and folders should get override.
how to solve this.
http://php.net/manual/en/ziparchive.open.php , on open, pass in the over write flag.

Corrupt ZIP files using PHP & PCLZip

I'm having some issues, I'm using PCLZip to create an archive. I don't get any errors, the zip file is created, but when I go to view it, the archive is empty and on my windows machine I get an error "The Compressed (zipped) folder "local directory zip file") is invalid. I have the following code:
$dir = '../downloads/liability/';
$archive = new PclZip($dir.'archive.zip');
$v_list = $archive->create($dir);
if ($v_list == 0) {
die("Error : ".$archive->errorInfo(true));
}
My directory structure is:
-admin
--liabilityDev.php (where the above code resides)
--index.php
--commission.php
-downloads
--liability
---one.pdf
---two.pdf
The end result is that in the liability folder, there is a file called archive.zip which contains the 2 pdf's but I get the invalid error.
If I don't have the directory variable, I archive index.php and commission.php and that works fine. It leads me to believe it may be a permission issue, but I'm running on fumes now. Please help!
You can try this:
if(extension_loaded('zip')){
$zip = new ZipArchive();
if($zip->open('../downloads/liability/archive.zip', ZIPARCHIVE::CREATE)===TRUE){
$zip->addFile('path of any normal file to be add into zip');
}
$zip->close();
}
I think, this will fullfill your need. Before implementing this code, please check first that, zip extension is already loaded or not.

PHP ZipArchive is zipping the lower directories also

I found this script on stackoverflow but am having an issue. The zip is being created with the file I am telling it to but it's also zipping the lower directories to that file. The zipped file contains: \uploads\1\assets\2\ai\filename.ai
$zip = new ZipArchive();
$zip->open('uploads/1/assets/2/ai/filename.zip', ZIPARCHIVE::CREATE);
$zip->addFile('uploads/1/assets/2/ai/filename.ai');
$zip->close();
$zip->addFile('uploads/1/assets/2/ai/filename.ai', 'filename.ai');
The second parameter is the localname:
If supplied, this is the local name inside the ZIP archive that will
override the filename.
If you leave off the directory path, it will not be included in the zip file.

Categories