最近帮助另一个用户得到了类似于工作的东西(没有随机选择),您可能会发现以下内容有用。这会搜索某个特定文件扩展名的目录,然后随机选择10个压缩并发送的文件。更改$sourcedir
和$ext
以适应 - 希望它有所帮助。
/* From David Walsh's site - modified */
function create_zip($files = array(), $destination = '', $overwrite = false) {
if(file_exists($destination) && !$overwrite) { return false; }
$valid_files = array();
if(is_array($files)) {
foreach($files as $file) if(file_exists($file)) $valid_files[] = $file;
}
if(count($valid_files)) {
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) return false;
foreach($valid_files as $file) $zip->addFile($file, pathinfo($file, PATHINFO_FILENAME));
$zip->close();
return file_exists($destination);
}
return false;
}
/* Simple function to send a file */
function sendfile($filename=NULL, $filepath=NULL){
if(file_exists($filepath)){
if(!is_file($filepath) or connection_status()!=0) return FALSE;
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
header("Content-Type: application/octet-stream");
header("Content-Length: ".(string)(filesize($filepath)));
header("Content-Disposition: inline; filename={$filename}");
header("Content-Transfer-Encoding: binary\n");
if($file = @fopen($filepath, 'rb')) {
while([email protected]($file) and (connection_status()==0)) {
print(fread($file, 1024*8));
flush();
}
@fclose($file);
}
return((connection_status()==0) and !connection_aborted());
}
}
/* Select a random entry from the array */
function pick($arr){
return $arr[ rand(0, count($arr)-1) ];
}
/* The directory to which the zip file will be written before sending */
$target=__DIR__.'\zipfile.zip';
/* The directory you wish to scan for files or create an array in some other manner */
$sourcedir = 'C:\Temp\temp_uploads';
/* File extension to scan for */
$ext='txt';
/* Placeholder to store files*/
$output=array();
/* Scan the dir, or as mentioned, create an array of files some other way */
$files=glob(realpath($sourcedir) . DIRECTORY_SEPARATOR . '*.'.$ext);
/* Pick 10 random files from all possible files */
do{
$rnd=pick($files);
$output[ $rnd ] = $rnd;
}while(count($output) < 10);
/* streamline array */
$output=array_values($output);
if($target) {
/* Zip the contents */
$result=create_zip($output, $target, true);
/* Send the file - zipped! */
if($result) {
$res=call_user_func('sendfile', 'zipfile.zip', $target);
if($res) unlink($target);
}
}
当我尝试下载链接时,我得到了一个带有21个空文件的zip存档! – RamRaider
顺便说一句 - 您网站上的html无效 - 没有为'upload'链接关闭'a'标签,'
Follow me!'然后是一个额外的伪造标签对在底部... – RamRaider