将php变量设置为*(全部)
我使用下面的代码创建和下载一个zip文件从所有的文件根文件夹。 我有这条线将php变量设置为*(全部),php,zip,Php,Zip,我使用下面的代码创建和下载一个zip文件从所有的文件根文件夹。 我有这条线 $files_to_zip = array( 'room1.jpg', 'room2.jpg' ); 它允许您选择要放入zip文件的文件,但我的文件是cron作业的结果,因此每天都会生成一个新文件。如何编写一个变量来表示除“”之外的所有文件,然后在zipfolder ie.index.php等中列出我不需要的文件 到目前为止,我试着写$files\u to\u zip=*(显然这不会排除任何文件),
$files_to_zip = array(
'room1.jpg', 'room2.jpg'
);
它允许您选择要放入zip文件的文件,但我的文件是cron作业的结果,因此每天都会生成一个新文件。如何编写一个变量来表示除“”之外的所有文件,然后在zipfolder ie.index.php等中列出我不需要的文件
到目前为止,我试着写$files\u to\u zip=*代码>(显然这不会排除任何文件),但这只会引发一个解析错误:语法错误
以下是完整的代码:
<?php
/* creates a compressed zip file */
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(
'room1.jpg', 'room2.jpg'
);
//if true, good; if false, zip creation failed
$zip_name = 'my-archive.zip';
$result = create_zip($files_to_zip,$zip_name);
if($result){
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=filename.zip');
header('Content-Length: ' . filesize($zip_name));
readfile($zip_name);
}
?>
对文件系统进行通配符匹配的函数是glob()
这不就是glob()
函数吗?@Barmar您可能应该发布一个答案来解释glob()
;)是的,谢谢@barmar,效果很好。。对不起,我仍然掌握着php的窍门。。但是谢谢你给我指出了正确的方向
$files_to_zip = glob("*");