php PHP片段
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php PHP片段相关的知识,希望对你有一定的参考价值。
<?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(
'upload/1_3266_671641323389_14800358_42187034_1524052_n.jpg', 'upload/1_3266_671641328379_14800358_42187035_3071342_n.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);
}
?>
// Set variable
$var = true; // false = false
$var = 1; // 0 = false
$var = "a"; // "" = false
// Return true if variable is greater than 0
//
// Case 1 - Faster and higher performence
//
echo ($var ? “true” : “false”);
// Case 2
//
($var) ? print “True” : print “False”;
以上是关于php PHP片段的主要内容,如果未能解决你的问题,请参考以下文章
php PHP:PDO片段
php PHP片段
php PHP片段
php PHP片段
php PHP片段
php Breadcrumbs片段php