...

View Full Version : Resolved Toxic Images in Folder Stop Script from Executing



bronzino
11-21-2010, 06:08 PM
Noob here. The script below looks through the targeted directory on my Mac and displays all of the images that are found there. I just discovered a problem with it and need some help and counsel to sort it out.

It appears (because I had one aberration) that the script is refusing to load images over say 2.5 MB. Is there a setting in the GD library (PHP 5.3.2) or something that I can reset to stop this from happening?

When one of the toxic images is loaded, the script terminates at the point ref'd below (right before the folder is closed ['fclose'] and the output file is written). Is there some kind of escape I can write so that the script keeps running even when it encounters these toxic images?

Thanks in advance for any help. I probably spent 40 hours on this darned thing and so I'm pretty disappointed it doesn't work as expected.

Elizabeth


<?php

function createGallery( $pathToImages )
{

$output .= "<table cellspacing=\"0\" cellpadding=\"2\" align=\"left\" style=\"margin: auto;\">";
$output .= "<tr>";

// open the directory
$dir = opendir( $pathToImages );
echo "dir variable is $dir and pathtoImgs is $pathToImages and".$_GET['ID']."<br/>";
$counter = 0;

// loop through the directory
while (false !== ($fname = readdir($dir)))
{
echo "looping through directory<br/>";

// parse path for the extension
$info = pathinfo($pathToImages . $fname);

//strips extension off file name for later use
//assumes file extension is three characters
$pos = strrpos($fname, '.');
$basename = substr($fname, 0, $pos);
$extension = substr($fname, $pos+1);

// strip the . and .. entries out and parse for valid jpg, gif, png
if (!preg_match('~^ref_~i', $basename) && (!preg_match('~fixed~i', $fname)) && (!preg_match('~1000~i', $fname)) && (!preg_match('~800~i', $fname)) && (!preg_match('~600~i', $fname)) && (!preg_match('~400~i', $fname)) && (!preg_match('~150~i', $fname)) && (strtolower($info['extension']) == 'jpg') || (strtolower($info['extension']) == 'png') || (strtolower($info['extension']) == 'gif'))
{
$output .= "<td valign=\"top\" align=\"center\">";

// load image and get image size
$imgjpg = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$widthjpg = imagesx( $imgjpg );
$heightjpg = imagesy( $imgjpg );
$imggif = imagecreatefromgif( "{$pathToImages}{$fname}" );
$widthgif = imagesx( $imggif );
$heightgif = imagesy( $imggif );
$imgpng = imagecreatefrompng( "{$pathToImages}{$fname}" );
$widthpng = imagesx( $imgpng );
$heightpng = imagesy( $imgpng );

$output .= "<a href=\"javascript: document.forms['frm_".$fname."'].submit();\"><img src=\"";
$output .= "{$pathToImages}{$fname}";
$output .="\"border=\"0\" width=\"100\" style=\"max-height: 75px;\"/></a><br />";

$output .= "<form name=\"frm_{$fname}\" action=\"local_photo_detail.php?ID=".$_GET['ID']."\" method=\"post\"><input name=\"Path\" type=\"hidden\" value=\"".$pathToImages."\"><input name=\"Basename\" type=\"hidden\" value=\"".$basename."\"><input name=\"Extension\" type=\"hidden\" value=\"".$extension."\"><input name=\"Width\" type=\"hidden\" value=\"".$widthjpg.$widthgif.$widthpng."\"><input name=\"Height\" type=\"hidden\" value=\"".$heightjpg.$heightgif.$heightpng."\"><input name=\"Filename\" type=\"text\" class=\"notfieldc\" value=\"".$fname."\"><br /><input name=\"Dimensions\" type=\"text\" class=\"notfieldc\" value=\"".$widthjpg.$widthgif.$widthpng."x".$heightjpg.$heightgif.$heightpng."\"><br /></form>";

$output .= "</td>";
// tells the table when to start a new row
$counter += 1;
if ( $counter % 7 == 0 ) { $output .= "</tr><tr>"; }
}

}

// ** script terminates unexpectedly here **
// close the directory
closedir( $dir );
$output .= "</tr>";
$output .= "</table>";
// open the file
$fhandle = fopen( "local_photo_gallery.htm", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
}
createGallery
("../".$_GET['ID']."/");

MattF
11-21-2010, 06:33 PM
Replace:



// strip the . and .. entries out and parse for valid jpg, gif, png
if (!preg_match('~^ref_~i', $basename) && (!preg_match('~fixed~i', $fname)) && (!preg_match('~1000~i', $fname)) && (!preg_match('~800~i', $fname)) && (!preg_match('~600~i', $fname)) && (!preg_match('~400~i', $fname)) && (!preg_match('~150~i', $fname)) && (strtolower($info['extension']) == 'jpg') || (strtolower($info['extension']) == 'png') || (strtolower($info['extension']) == 'gif'))
{


with:



// strip the . and .. entries out and parse for valid jpg, gif, png

$ext = array('gif', 'jpg', 'jpeg', 'png');
$regex = array('fixed', '1000', '[468]00', '150');

if (!preg_match('~^ref_~i', $basename) && !preg_match('~'.implode('|', $regex).'~i', $fname) && in_array(strtolower($info['extension']), $ext) && filesize($info) < (1024*2400))
{


The filesize() check, (if my math is correct), should temporarily stop it processing files above 2.4Mb until you find the cause of the size issue.

bronzino
11-21-2010, 06:47 PM
Thank you for your help. :)

I did the replacement and the script is still terminating early, Matt. I adjusted the "1024*2400* size downward to see if that would change my results, but it seems to be the same.

I believe Satan is at work here. :mad:

MattF
11-21-2010, 06:57 PM
Is it printing the name of which file it's balking at, before it exits? Is it always the same file and are other files actually being processed fine? Does it run to completion if you remove the offending image(s)?

bronzino
11-21-2010, 07:23 PM
It runs to completion if I remove the offending images.

What it seems to do is go through the directory ($pathToImages), getting the attributes of various files, and then stops when it encounters an offending image. It then aborts the script so that the output file (which displays the images) never generates. I have a version of the above script going with some clunky "echo" error reporting to show me where the script stops. When image PA020001.jpg (2.5 MB) is introduced into the directory, this is what I see:

dir variable is Resource id #14 and pathtoImgs is ../330/
looping through directory

PA020000.JPG position 1
PA020000.JPG position 2
PA020000.JPG position 3
PA020000.JPG position 4
PA020000.JPG position 5
looping through directory

Since PA020001.JPG is next, the script stops working at that point, even though PA020001.JPG meets the script's criteria otherwise, and even though there are images following PA020001.JPG in the directory.

I redid the script, stripping it down to what I think are its component parts (I didn't write it), and I got another version of this data:

pathinfo Array
fname .
dir Resource id #14
pathtoImages ../330/

pathinfo Array
fname PA020000.JPG
dir Resource id #14
pathtoImages ../330/

pathinfo Array
fname PA020001.JPG
dir Resource id #14
pathtoImages ../330/

In this second version, it does make reference to PA020001.JPG, but as you can see, again, it stops there. No other files are recorded.

bronzino
11-21-2010, 07:49 PM
Maybe it IS Satan:

http://bugs.php.net/bug.php?id=27792

If anybody can find the patch that's referenced in the last comment, I'd love to try it.

Or can someone direct me to an alternative way of doing this other than reliance on readdir?

bronzino
11-21-2010, 08:03 PM
Found an alternative, and now I can say with nearly complete certainty that it was the bug with readdir causing my issues. Scandir works better. Here's a sample script I found by Googling, which works like a champ:


<?php

// directory view
$folder = '../330/';
$files = scandir($folder);
//$fcount = count( $files );

echo '<body bgcolor="#DDDDDD">';
echo "<b><i>DIRVIEW FILE INDEX</i></b>";
echo "<hr>";
foreach ( $files AS $name ){
if(substr($name,0,1)!=".")
echo '<img src="'.$folder.$name.'" target="blank">'.$name.'</img><br>';
}
echo '</body>';
exit; ?>Thanks again, Matt. Talking through it with you here helped me figure it out. :)

I think I shall go have a cinnamon roll to celebrate.

MattF
11-21-2010, 09:36 PM
Good to hear you found a cure. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum