アルバムの年などの基準に基づいていくつかの曲をリストする 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'..)
正常に動作します。
私のコードのバグは何ですか?