Strange characters after extension creating zip file in php - IIS - php

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

Related

Laravel : Extract ZipArchive file from storage external disk

I'm trying to extract a ZipArchive file located in an external storage disk in my laravel application. It is working on my local environment but not in production
Edit : my FILESYSTEM_DRIVER env variable is different on production and working (tested with others Storage functions)
I have a zip file located in Storage::path('folder/file.zip'); which i'm trying to extract in the same folder :
// path to the zip file
$path = Storage::path('folder/file.zip');
$zip = new ZipArchive;
if($zip->open($path) {
$zip->extract(Storage::path('folder')); // working in local but not in production on an external disk
$zip->close();
}
With this code, I receive a code 9 error : No such file
It says that my $path variable is wrong but I my debug Storage::exists('folder/file.zip'); returns true
I don't know where I am missing something. I could not find any helping answer on the web.
Any help would be appreciated.
Thanks in advance
$path = Storage::path('folder/file.zip');
$zip = new ZipArchive;
if($zip->open($path)) {
$zip->extract(Storage::path('folder')); // working in local but not in production on an external disk
$zip->close();
}

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 - adding only files not the parent folder

I am creating a zip archive in php and want to add some pdf files available in my pdfs_lib folder.I have successfully created the archive but there is a problem that inside the created zip archive, files get added into pdfs_lib folder, which is not what i want.
I want to add pdf files present in pdfs_lib folder directly into the archive instead of adding those files inside pdfs_lib folder in my created zip archive.
What i have tried is:
// Create Zip File
$zip = new ZipArchive;
$res = $zip->open("pdfs_lib/my_zip_archive.zip", ZipArchive::CREATE);
if ($res === TRUE)
{
// Add pdf file to zip archive
$zip->addFile("pdfs_lib/pdf_file_1.pdf");
$zip->addFile("pdfs_lib/pdf_file_2.pdf");
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
This code creates the zip archive successfully but whwen i unzip the archive it creates a pdfs_lib folder and unzip all files inside this newly created folder.
Any suggestion on how to create a zip archive without add pdf_lib folder in it.
Change the addFile calls to:
$zip->addFile("pdfs_lib/pdf_file_1.pdf", "pdf_file_1.pdf");
The second parameter is the 'localname', the name used in the zipfile itself. If you leave out the folderpath there, it will be added to the root in the zipfile.
A side not as an into: usually it is considered good practice if an archive (be it zip or any other archive format) unpacks into a clean folder instead of littering the contained files all over the place.
However if you really want to implement this without such folder, then you must rework your code. You have two alternatives:
you can change the working directory your php script is executed in into that folder where you keep the files in. In that case you do not need any path to add files to the zip archive, thus no folder is constructed inside the archive.
the addFile() method of phps zip extension allows to specify a second (optional) argument which allows to pass the target name of a passed file.

Move uploaded file to root directory

I'm making a content management system for a website I built. I want the system to be discrete, so I made it exist in only one PHP file, called '_admin.php'. All the content displayed in this file comes from includes that I store in a sub-folder called 'admin' (out of the way).
The photos used on the website are stored in an 'assets' folder that also sits in the root dir. The admin page has direct access to the assets folder, as it is also in the root. But the upload file script sits a few directories into the 'admin' folder and I want the uploaded files to be stored in the assets folder.
The move_uploaded_file() method takes the destination path for the file, but it requires a direct path. I try using $_SERVER['DOCUMENT_ROOT'], but the resulting directory doesn't seem to have any of my files. If I use getcwd() in a doc in the root, it returns the actual file structure that I can use. The same if I echo out __FILE__. But I've experimented with this SERVER constant a lot and I can't locate my website with it.
Since the script that uploads the images is called as a form action, I can't pass the root directory as a variable.
Not really sure what I'm doing wrong, anyone have any ideas?
Thanks
edit **
//See if Files array contains new files
if (!empty($_FILES['file'])){
foreach($_FILES['file']['name'] as $key => $name){
$error = $_FILES['file']['error'][$key];
$temp_name = $_FILES['file']['tmp_name'][$key];
$dir = getcwd();
$move_file = move_uploaded_file($temp_name, "$dir/temp/$name");
if (($error == 0) && ($move_file)){
$uploaded[] = $name;
}else{
die($error);
}
}
echo __FILE__;
echo "<br/>";
echo __DIR__;
echo "<br/>";
echo $_SERVER['DOCUMENT_ROOT'];
exit();
}
The upload script I'm currently using. This script works fine because I'm storing the images in the same directory as the script. I just don't know how to store them in my root.
I would specify the full absolute file path if you can. I set this via define() in a config file for my CMS. On install you figure out what that path is and set it.
define("BASEFILEPATH", "/home/.../[webroot]"); // The base file path for the website
You may be looking for something more general, but you could have some sort of install script where the user can enter basic info into a form, such as username, pwd, etc. and you could have them enter this path as well.

Categories