Jump to content
Larry Ullman's Book Forums

Recommended Posts

PG.618 Chap 19

 

It just occurred to me if multiple users uploading pictures and then they are renamed to have their own unique md5 name. We can't assume here that everyone would upload a jpeg, what about all the other picture formats, and if we save them as an md5 name, how would we know which type of picture format to load up? Anyone here have any experience with this situation.

Link to comment
Share on other sites

Just post up the Show_image.php script in case others want to see the solution

 

<?php # Script 19.8 - show_image.php
// This pages retrieves and shows an image.
// Flag variables:
$image = FALSE;
$name = (!empty($_GET['name'])) ? $_GET['name'] : 'print image';
// Check for an image value in the URL:
if (isset($_GET['image']) && filter_var($_GET['image'], FILTER_VALIDATE_INT, array('min_range' => 1)) ) {
// Full image path:
$image = '../uploads/' . $_GET['image'];
// Check that the image exists and is a file:
if (!file_exists ($image) || (!is_file($image))) {
$image = FALSE;
}

} // End of $_GET['image'] IF.
// If there was a problem, use the default image:
if (!$image) {
$image = 'images/unavailable.png';
$name = 'unavailable.png';
}
// Get the image information:
$info = getimagesize($image);
$fs = filesize($image);
// Send the content information:
header ("Content-Type: {$info['mime']}\n");
header ("Content-Disposition: inline; filename=\"$name\"\n");
header ("Content-Length: $fs\n");
// Send the file:
readfile ($image);

 

The getimagesize() function will determine the size of any given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondant HTTP content type.

Link to comment
Share on other sites

 Share

×
×
  • Create New...