TechMistress Posted January 11, 2010 Share Posted January 11, 2010 Hello all, I have a question about checking the filesize of images. On the script, I have various users, who have various numbers of listings, and each of these listings have one or more images. On the one page where they see an abbreviated list of all of their listings, I want to warn which ones contain files that may be too large. This is the code I have: $MyImages = explode("|", $ap[image]); while(list(,$vi) = each($MyImages)) { $size = filesize('re_images/'.$vi); if( $size > 153600 ) { echo '<p align=center><img src="images/warning2.gif" border="0"></p>'; $warning="<font color=#FF0000 size=5>!!</font></b>"; } The problem is, I think it is counting all of the images for all of the listings, which would be over the limit. So, all listings are marked with the error (!!)and the warning2.gif shows up 10 times at the top of the page. I'm guessing what the problem is, so I'm not sure. Do I need to put code in there to say if any INDIVIDUAL image is over limit, or what? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/188088-filesize-question/ Share on other sites More sharing options...
laffin Posted January 11, 2010 Share Posted January 11, 2010 to me the problem code is here while(list(,$vi) = each($MyImages)) why use while & list? replace with foreach($MyImages as $vi) Quote Link to comment https://forums.phpfreaks.com/topic/188088-filesize-question/#findComment-993065 Share on other sites More sharing options...
TechMistress Posted January 11, 2010 Author Share Posted January 11, 2010 Ok, so I replaced that code to look like this: $MyImages = explode("|", $ap[image]); foreach($MyImages as $vi) { $size = filesize('re_images/'.$vi); if( $size > 153600 ) { echo '<p align=center><img src="images/warning2.gif" border="0"></p>'; $warning="<font color=#FF0000 size=5>!!</font></b>"; } However, it's still doing the same thing. It's marking EVERY listing as having images too large, and it's putting up the warning message 15 times (as many as the number of listings they have. It may be that I cannot do this, I'm guessing. Since all images for all listings are in one folder, I'm trying to ask the page to look at the database and see this client's id, then look at each of the client's listings, then look at each of the images attached to each of those listings, and determine if any of them are over sized. It apparently looks like, perhaps, it's adding them up? Because the client I'm testing on has no images oversized. Quote Link to comment https://forums.phpfreaks.com/topic/188088-filesize-question/#findComment-993139 Share on other sites More sharing options...
mikesta707 Posted January 11, 2010 Share Posted January 11, 2010 The foreach{} construct and while(list(x) = each(y)) construct are exactly the same, so replacing one with the other will do nothing. the logic in that code seems fine. You do realize that filesize() returns returns the size in bytes right? is ~150kb what you expect most images to be under? Check your ftp/where ever the images are stored, and verify that the file sizes are what you think they are. if not, you may have to alter your if statement Quote Link to comment https://forums.phpfreaks.com/topic/188088-filesize-question/#findComment-993162 Share on other sites More sharing options...
TechMistress Posted January 11, 2010 Author Share Posted January 11, 2010 Yeah, I checked their files, and they are all in fact under 20k. When I did this on the edit page of their listings, it all works fine, because I have the images displayed on the page (while list code), and if one is too large, it shows the error beneath that image. however, on this manage page, there are no images called or displayed, it is a list of links to each of the listings: listingid1 listingid2 listingid3 Quote Link to comment https://forums.phpfreaks.com/topic/188088-filesize-question/#findComment-993177 Share on other sites More sharing options...
mikesta707 Posted January 11, 2010 Share Posted January 11, 2010 alright, in that case, try printing the value of $vi to the screen. Make sure those are what you expect them to be. also print $size to the screen and verify that is what you expect it to be. also try turning error reporting on by adding the following to the top of the page error_reporting(E_ALL); ini_set("display_errors", 1); Quote Link to comment https://forums.phpfreaks.com/topic/188088-filesize-question/#findComment-993181 Share on other sites More sharing options...
TechMistress Posted January 11, 2010 Author Share Posted January 11, 2010 well, it's doing what I suspected it might. When I print size, it's giving me 290816 - which is the size of all the images on all 15 of her listings combined. I didn't do error reporting, since I'm not getting any errors. The code is doing what I told it to perfectly. Unfortunately, I'm telling it to do the wrong thing. Quote Link to comment https://forums.phpfreaks.com/topic/188088-filesize-question/#findComment-993204 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.