I'm using this function to zip files.
function zip($path, $file){
$zipname = getFilename($path.$file).'.zip';
$zip = new ZipArchive();
$zip->open($path.$zipname, ZipArchive::CREATE);
$zip->addFile($path.$file, $file);
$zip->close();
return $zipname;
}
Lately, I found that it has an error with files contain utf-8 content (filename also). The result is something similar to:
Thùc hiÖn c«ng v¨n sè 775-CV/QU, ngµy 30 th¸ng 11 n¨m 2009 cña
QuËn uû §å S¬n vÒ viÖc chuÈn bÞ b¸o c¸o t×nh h×nh
How could I fix it?
Related
I am working on zip file creation and downloading using PHP (Laravel).
Zip created and download works well. But the problem is in lhaplus software. When extracting the zip file using lhaplus, it has been changing the file name. But in another extractor like 7zip, WinRAR works well. In lhaplus software shows "03510063220_00016501tñ+sÉìtñ+sÉì_spt.pdf" but it should be 03510063220_00016501社名社名_spt.pdf.
Please help in this case. Any suggestion is appreciated.
The code is given below:
$date_time = static::getCurrentTime();
$zip_name = $date_time."_spt";
$zip = new ZipArchive;
if(!file_exists('zip/downloadedZip')){
mkdir('zip/downloadedZip',0777,true);
}
$zipFileName = 'zip/downloadedZip/'.$zip_name.'.zip';
if ($zip->open(public_path($zipFileName), ZipArchive::CREATE) === TRUE){
$fileName = "pdf/03510063220_00016501社名社名_spt.pdf";
$baseName = "03510063220_00016501社名社名_spt.pdf";
if (!$zip->addFile($fileName, mb_convert_encoding($baseName, "SJIS", "UTF-8"))) {
throw new \RuntimeException(sprintf('Add file failed: %s', $fileName));
}
$zip->close();
}
return URL($zipFileName);
Imagine there is a picture at http://example.com/icon.jpg and I want to add it to a zip file on my own sever named "Stack.zip" using php. This is my code, but it doesn't work.
$url="http://example.com/icon.jpg"
$zip = new ZipArchive;
echo $zip->open("Stack.zip");
$zip->addFile($url);
$zip->close();
P.S. I was able to do it with local files, but I had no success on doing it with internet addresses. So that's why I asked this question.
From the PHP manual: http://php.net/manual/en/ziparchive.addfile.php
(PHP 5 >= 5.2.0, PHP 7, PECL zip >= 1.1.0)
This assumes you want to add to an existing zip file on your server.
$url = 'https://www.stackoverflowbusiness.com/hubfs/logo-so-color.png?t=1499443352566';
$local_path = '/your/local/folder/';
$img = 'icon.jpg';
file_put_contents($local_path.$img, file_get_contents($url));
$zip = new ZipArchive;
if ($zip->open($local_path.'Stack.zip') === TRUE) {
$zip->addFile($local_path.$img, $img);
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
If you want to keep the same folder structure as the original source file, change this line ..
$zip->addFile($local_path.$img, $img);
...to this...
$zip->addFile($local_path.$img);
If you want the same script to create the zip, you can find a PHP function here: Add files to the zip
I have used ZipArchive for creating zip. but i have problem with filename assign in Arabic language that is not supported.
I want to get filename like 21_بينل.pdf but i getting 21_.pdf.
I have refer following question.
PHP ZipArchive non-English filenames return funky filenames within archive
but still not working.
My sample code:
<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
$fileName = $v->certificateLetter->fileName.'/'.$v->dslStu->stu_unique_id.'_'.'بينل';
$zip->addFromString($fileName.'.pdf', $letterString);
$zip->close();
?>
Thanks in Advance.
Try this:
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
$fileName = $v->certificateLetter->fileName.'/'.$v->dslStu->stu_unique_id.'_'.'بينل';
iconv('CP1256', 'utf-8', $fileName).PHP_EOL;
$zip->addFromString($fileName.'.pdf', $letterString);
$zip->close();
I tried to extract a zip file using ziparchive, here's my code:
$path = "/test/admin";
$zip = new ZipArchive;
if ($zip->open("test.zip") === TRUE) {
$zip->extractTo($path."/", array("index.php"));
$zip->close();
}
but the output becomes /test/admin/index.php/.
The file becomes a folder.
Can someone help me with this.
Would appreciate some help on this. I've been trying to write a PHP script that Unzips a zip file that has been created using PHP's in-built Zip Archive extension.
The zipping-up process has been very straight forward but now I'm trying to unzip this to a particular folder and it doesn't seem to be working. The only thing it does is create the folder I've asked it to extract to. No files appear in that folder.
I've had no error messages.
Thanks in Advance. Here's my code:
<?php
$root = str_replace('public_html', '', $_SERVER["DOCUMENT_ROOT"]);
$path = $root.'scripts.zip';
$zip = new ZipArchive;
$zipped = $zip->open($path, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
$folder = $root.'public_html/scripts/';
if ($zipped) {
$extract = $zip->extractTo($folder);
if ($extract){
echo 'Zip File Extracted';
}
$zip->close();
}
?>
I would modify your code to look like the one below.
What you were doing was creating a new zip file, not extracting the old one. ZIPARCHIVE::CREATE instructs the code to create a zip, ZIPARCHIVE::OVERWRITE tells it to overwrite existing files in the zip.
So you are not extracting anything from the zip. This code below should work
<?php
$root = str_replace('public_html', '', $_SERVER["DOCUMENT_ROOT"]);
$path = $root.'scripts.zip';
$zip = new ZipArchive;
$zipped = $zip->open($path); /*I have removed the ::CREATE & ::OVERWRITE methods*/
$folder = $root.'public_html/scripts/';
if ($zipped) {
$extract = $zip->extractTo($folder);
if ($extract){
echo 'Zip File Extracted';
}
$zip->close();
}
?>
Hope that helps