Extract Zip File from remote server with white-space in file name - php

I have a list of zip files to read from a remote server. The list of the files are as follows:
CA-EN Bosch 20170901.zip
CA-EN Gaggenau 20170901.zip
CA-EN Thermador 20170901.zip
As we can see here, all filenames are with white-space in it. Due to this My code is unable to extract these zip files.
Following is what I am using to extract file contents:
$destination = '../../boschzip';
$zip = new ZipArchive;
$res = $zip->open('CA-EN Bosch 20170901.zip'); // Your filename
if ($res === TRUE) {
$zip->extractTo('../../boschzip');
$zip->close();die;
}
else{
echo 'Error Message. :(';die;
}
I can not make any change in the file name as it is on the client's server. So is it possible to extract these zip files with white-space in their file names.

Related

How to copy the source code of my website recursively into a zip file and download using PHP

Since my file manager doesn't allow me to download multiple files, instead only allowing me to download them one by one (which is tedious, and, eventually will become inefficient), I want to know how to download all my website file contents into a single zip folder. I found a code that works from geeksForGeeks, however it only zips on that current directory level (not recursively). I want every file on my website put into a zip folder while preserving their place in their corresponding folders.
The code I found:
// Enter the name of directory
$pathdir = "./";
// Enter the name to creating zipped directory
$zipcreated = "BackupFiles.zip";
// Create new zip class
$zip = new ZipArchive;
if($zip -> open($zipcreated, ZipArchive::CREATE ) === TRUE) {
// Store the path into the variable
$dir = opendir($pathdir);
while($file = readdir($dir)) {
if(is_file($pathdir.$file)) {
$zip -> addFile($pathdir.$file, $file);
}
}
$zip ->close();
}
How do I add the folders as well? It only zips the files of the current directory and not all the subfolders.

Not able to read file in php

I am facing the issue with this code:
<?php
$files = scandir("D:/Dummy");
foreach($files as $file) {
$filenam = $file;
$path_to_file = $filenam;
$file_contents = file_get_contents($path_to_file);
echo "Hello ".$filenam;
$printFileName="";
if(strpos("9222339940", $file_contents) === false)
{
$printFileName=$filenam." ";
}
}
echo $printFileName;
?>
Basically, I have written this code to scan all the files in the directory and from the each file, I need to replace the mobile number. But for some reason, I'm not able to run the script. It is throwing error:
file_get_contents(name of the file) failed to open stream. No such file or directory error.
The scandir() function of PHP will only return the basenames of the files within the directory. That is, if your directory D:\Dummy contains a file test.txt, then scandir() will not return the full path D:\Dummy\test.txt, but only test.txt. So the PHP process will not find the file, because you need to provide the complete path of the file.

PHP how to remove author metadata from docx

In PHP I've to remove the author metadata from a DOCX file. First of all, I've unzip the document and after I've edit the author metadata setting it empty using an XML reader library.
$file = 'document.docx';
$filename = 'path/'.$file;
// Unzip the docx
$unzipped = md5($filename);
if (file_exists($unzipped)) {
rmdir($unzipped);
mkdir($unzipped);
}
$zip = new ZipArchive;
$res = $zip->open($filename);
if ($res === true) {
extractFolder($zip, "word/media", $unzipped);
$zip->extractTo($unzipped);
$zip->close();
} else {
die("The docx file appears to be corrupt (i.e. it can't be opened using Zip).\n");
}
The problem occurs when I tried to re-zip the document. In fact, when the re-zip is done, the docx file seems to be corrupted when I open it in Word.
create_docx($unzipped,'_zips/'.$file );
How can I modify the author metadata (setting the document anonymous) and save the correct docx file?
I don't know php, but I assume the problem is that you have to compress inside the folder $unzipped, NOT the folder itself. So a kind of recursive "$unzipped/*" as source instead of "$unzipped".

Creating zip file with php

I am creating a zip file of a particular folder, I am using joomla 1.5 version in which i have a component of form submission and I want to create a zip file with submitted documents. On my localhost, its creating the zip file, when I test to remote server, It replaces the extension with some kind of a number, suppose we are creating a file test.zip, but its creating like this test.zip.a12345 and test.zip.b12345..
The code which I have used to create zip file:
$files = array(
'files/file1.jpg',
'files/file2.jpg',
'files/file3.jpg'
);
$zip = new ZipArchive();
$zip_name = "zipfile.zip";
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
$error .= "* Sorry ZIP creation failed at this time";
}
foreach($files as $file){
$zip->addFile($file);
}
$zip->close();
and the output on local server is zipfile.zip
output on live server is : zipfile.zip.a11236 and zipfile.zip.b11236
You probably need to use the ZipArchive::OVERWRITE flag when opening the file.
It chooses another name because the file already exists?
See: http://php.net/manual/en/zip.constants.php

How we can read zip file and get information of files or folders contains without unzipping in PHP?

What I actually wanted to do is read zip file and then if it does contain folder then refuse it with some message.
I want user should upload zip file with files only without any directory structure.
So I want to read zip file contains and check file structure.
I am trying with following code snippet.
$zip = zip_open('/path/to/zipfile');
while($zip_entry = zip_read($zip)){
$filename = zip_entry_name($zip_entry);
//#todo check whether file or folder.
}
I have sorted out.
I am now checking filename as strings wherever I am getting string ending with "/" that am treating as directory else as file.
can't you parse path of $filename? something like $dirName = pathinfo($filename, PATHINFO_DIRNAME)

Categories