I am trying to add files into a zip by sending the file names from a HTML form.
The HTML code is
<form action="act.php" method="POST">
<input type="checkbox" name="send1" value="filename1.extension">
<input type="checkbox" name="send1" value="filename2.extension">
</form>
The PHP
I tried in two ways
Method 1:
$zipfilename="abcd.zip";
$zip= new ZipArchive();
$zip->open($zipfilename, ZipArchive::CREATE);
foreach($_POST['send1'] as $filename)
{
$zip->addFile($filename);
}
$zip->close();
Method 2:
$zipfilename="abcd.zip";
$filestozip=array();
foreach($_POST['send1'] as $filename)
{
$filestozip[]=$filename;
}
$zip= new ZipArchive();
$zip->open($zipfilename, ZipArchive::CREATE);
foreach($filestozip as $filename2)
{
$zip->addFile($filename2);}
}
$zip->close();
But the Zip File is not getting created. I tried to check the Zip syntax with static code for $filetozip as
$filestozip[0]="filename1.extension";
$filestozip[0]="filename1.extension";
It worked. But when the data is from another HTML page dynamically it does not work. What is the error in my code?
Found the problem. There is no problem in my logic.
As I said previous the data for the $filename variable is received from a HTML for in which any spaces are replaced by %20 intentionally by me. The reason for such replacement is to enable links to the file using the data. But if the space is replaced by %20 zipArchive::addFile() does not add the zip file which ultimately result in Zip creation fail.