title: Neater way to find and display image file from ZIP archive with PHP
body: <pre><code>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."; }
  }
  </code></pre>
  
  <p>I'm running this function to open a specified zip file and then looping through the contents to find the specified file name, then creating an image from that file without the need to extract. I was just kind of curious how system intensive this process was, and if there was a neater/more direct manner of finding a file within a zip archive without the need to loop through and see if the name matches the given file name. Is there anyway to call a file directly from the zip file with the given name, assuming it exists?</p>
  
  <p>The above code works... I guess I'm just trying to see how to do it better. If that makes sense.</p>