Page 1 of 1

Need help with this bit of code please

Posted: Sun Jan 14, 2007 6:24 am
by tonto68
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,  I'm a total rookie at this.  I have a script which generates a random image from a file ( of images uploaded by my forum users) .  It works fine except one user uploads a .png image with all of his posts and its' showing up too frequently.  I thought I had the script pulling just jpg and jpeg images but for some reason this .png shows up.  I'm trying to modify the code to block or at least limit this .png image from showing up.  The file is also 541 bytes(?) if that helps.  My code is below.  Please let me know how I should modify this...thanks!

Code: Select all

<?php
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
Header("Pragma: no-cache");
Header("Content-Type: image/jpg");
Header("Content-Type: image/jpeg");


$dir = "forums/uploads/images"; // This is the folder where the images are

srand((double)microtime()*1000000);
$i = 0;
$dirHandle = opendir($dir); // Open the images folder
while(($im = readdir($dirHandle)))
{
if($im != ".." && $im != ".") // Don't read in the 2 folders ".." and "."
{
$image[$i] = $im; // Select an image
$i++;
}
}
closedir($dirHandle); // Close the folder
$n = rand(0,(count($image)-1));

if(!readfile($dir."/".$image[$n])) // Read the image
readfile($dir."error/error.gif"); // If the script can't find the directory, display this image
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Jan 14, 2007 6:30 am
by s.dot
Check for the type of the image using getimagesize(). The type of the image will be in index 2 of the resulting array.

For example, this will allow only jpg images.

Code: Select all

$img_info = getimagesize($image);

if($img_info[2] != 2)
{
     //warn that it must be jpg only
}

Posted: Sun Jan 14, 2007 8:01 am
by Ollie Saunders
tonto68, please edit your original message to conform with forum rules. You should have a subject that describes your problem and use php bb tags when posting code.

@Scottayy: I would prefer:

Code: Select all

$info = getimagesize($image);
if ($info['mime'] != 'image/jpg') {
    // not jpg
}
because then you don't have to look up in the manual what 2 is.
They really should have constants for those or return strings, pretty shoddy imo.

Where do I put this code?

Posted: Sun Jan 14, 2007 12:48 pm
by tonto68
Thanks for your replies...

I'm not sure what to do or where to put this code you have given me...Do I add it somewhere to the code I supplied?

Thanks.

Posted: Sun Jan 14, 2007 4:50 pm
by Ollie Saunders
You can pass the same parameter you passed to readfile()

Code: Select all

if(!readfile($dir."/".$image[$n]))
to getimagesize().