function showimage($zip_file, $file_name) {
if (file_exists($zip_file)) {
$zip = zip_open($zip_file);
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip, $zip_entry, "r")) {
if (zip_entry_name($zip_entry) == $file_name) {
$theimg = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
$theimg = imagecreatefromstring($theimg);
if ($theimg !== false) {
header('Content-Type: image/jpeg');
imagejpeg($theimg);
imagedestroy($theimg);
}
else { echo "Could not create image."; }
zip_entry_close($zip_entry);
}
}
else { echo "Could not open."; }
}
zip_close($zip);
}
else { echo "File not found."; }
}
Run Code Online (Sandbox Code Playgroud)
我正在运行此函数来打开指定的zip文件,然后循环遍历内容以查找指定的文件名,然后从该文件创建图像而无需提取.我有点好奇这个过程是如何系统密集的,如果有一个更整洁/更直接的方式在zip存档中查找文件而不需要循环查看名称是否与给定的文件名匹配.无论如何直接从具有给定名称的zip文件调用文件,假设它存在?
上面的代码有效...我想我只是想看看如何做得更好.如果这是有道理的.
我正在循环中检查字符串是否等于另一个字符串.简单的东西.
但是,似乎我一直在添加要检查的字符串,并且有十个不同的字符串,我正在检查每个循环.创建一个要检查的字符串数组更简单,然后执行in_array();但是我想知道哪个会更快地解析并使用更少的系统资源?
排列
$hideme = array(".", "..", "Thumb.db", "index.php", "icons", "index_backup.php",
"style.css", "highlighter.css", "highlighter.js", "users");
if (!in_array($sub, $hideme)) {
Run Code Online (Sandbox Code Playgroud)
String!= String
if ($sub != "." && $sub != ".." ...etc
Run Code Online (Sandbox Code Playgroud)
差异可能微不足道,只是好奇以备将来参考.