0

我有 PHP 页面,其中列出了一些基于专辑年份之类的标准的歌曲。我只是在每首歌曲附近放置了一个复选框。当访问者选中某个复选框并按下提交按钮时,表单会将每个值发送到 php 文件。在 PHP 文件中,值存储在一个数组中。

$files_to_zip=array();
for($i=0;$i<count($_POST['song'];i++)
$files_to_zip[]=$_POST['song'][$i];

//'song' is the name of the checkbox in the HTML form

问题是 PHP 文件没有使用我编写的代码创建 zip 文件。

function create_zip($files = array(),$destination = '',$overwrite = true) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

//close the zip -- done!
$zip->close();

//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}

$files_to_zip = array();
foreach($_POST['song'] as $fileabc)
{
$files_to_zip[]=$fileabc;
}
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.zip');
if($result==true)echo "Success"; else echo "failed";

这是我在 PHP 中使用的全部代码。

如果我动态设置 $files_to_zip 的值,例如,

$files_to_zip=array('link1','link2','link3'..)

它工作正常。

我的代码中的错误是什么?

4

0 回答 0