Need help with this bit of code please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tonto68
Forum Newbie
Posts: 2
Joined: Sun Jan 14, 2007 6:15 am

Need help with this bit of code please

Post 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]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
tonto68
Forum Newbie
Posts: 2
Joined: Sun Jan 14, 2007 6:15 am

Where do I put this code?

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

You can pass the same parameter you passed to readfile()

Code: Select all

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