Problem accessing Array

var_dump() is NOT failing!

Look at this snippet…


	$tempFile = $_FILES['userPhoto']['tmp_name'];

	if (empty($errors)){
		echo 'VAR_DUMP OF: getImageSize($tempFile)';
		var_dump(getImageSize($tempFile));
exit();

Results in this successful output…

$Filename = MySpreadsheet.xls

$tempFile = /Applications/MAMP/tmp/php/phpWwNJAB
VAR_DUMP OF: getImageSize($tempFile)

boolean false

What is causing the Error (Technically it is a “Notification”) is this line of code…


	if ($imageDetails = getimagesize($tempFile)){

But only when I try to upload my empty file called “00.txt”

Still doubt me? Look…


	$tempFile = $_FILES['userPhoto']['tmp_name'];

	if (empty($errors)){

//		echo 'VAR_DUMP OF: getImageSize($tempFile)';
//		var_dump(getImageSize($tempFile));
//		exit();

		if ($imageDetails = getimagesize($tempFile)){

When I try and upload “MySpreadsheet.xls” I get this…

is not an image

And when I try and upload “00.txt” I still get…

Notice: getimagesize() [function.getimagesize]: Read error! in /Users/user1/Documents/DEV/++htdocs/05_Debbie/members/upload.php on line 86
Call Stack
#	Time	Memory	Function	Location
1	0.0009	75000	{main}( )	../upload.php:0
2	0.0020	81196	getimagesize ( )	../upload.php:86


The latter one might be returning an empty string, but an empty string, empty array, 0, or null ALL == false – and they do not === false… which is why you should be doing the if statement on the ASSIGNMENT so that you don’t even TRY to use $imageData if the result is false… That’s where most of your issues lie, is you are assigning it, then trying to operate on it without ever testing it… TEST FIRST, if false, you have NO BUSINESS trying to use the result!

I get what you are saying, but I changed my code and that isn’t the issue…

As I’ve said several times here:


   $width = $imageDetails[0];
        $height = $imageDetails[1];
        $imageType = $imageDetails['mime']; 

SHOULD NOT EVEN BE ATTEMPTED if $imageDetals=getimagesize($filename) returns false… which is why you test that FIRST before trying to do ILLEGAL variable accesses.

It has been fixed…

Basically:


if ($imageDetails=getimagesize($filename)) {
	// do whatever you want/need to do with imageDetails here
} else {
	// false condition, you have NO business trying to use imagedetails for ANYTHING
	// BECAUSE IT FAILED!
}

I didn’t fix my code last night because of SitePoint freaking out…

You aren’t trapping it that way, and that’s why it’s not working for you.

See above.

So to re-cap, the $10,000 question is “Why doesn’t getimagesize() return FALSE on the file 00.txt but does on other non-Image files??” And more so, how do I handle that annoying Notification that is causing all of the issues??

Debbie

Generally PHP Notices can be ignored.

To do it, you need to modify the php.ini.

error_reporting = E_ALL & ~E_NOTICE

Also just want to say this thread appears to have blown up to the point that it’s difficult to follow.

I know that, but usually there is a way to change your code to get rid of the E_NOTICE (e.g. Undefined Index).

If I add back my @ then that fixes things (as long as I don’t have another error that I’d want to see during debugging)…

Also just want to say this thread appears to have blown up to the point that it’s difficult to follow.

I agree. :frowning:

To many people jumped in and went down too many paths…

All I know is that I am stuck on getimagesize()

Debbie

You can also do it with

ini_set(‘error_reporting’, E_ALL & ~E_NOTICE);

in your PHP if you only want it for this script.

I made a empty 00.txt and … oh, I see… That’s related to the same error as exif_imagetype I think, where anything smaller than three bytes bombs (wonder if getimagesize actually calls exif_imagetype or if they share some code?)…

So… your original approach of supressing the error with @ and then using what I’m saying for checking for false early should handle that… or you could do a filesize check.

if ($imageData=@getimagesize($tempFile)) {

since that still returns false on error, the error supression isn’t that bad a thing – unlike what you had where you weren’t trapping the result and just started working with the values…

Probably why mine reads:


		if (
			file_exists($sourceFile) &&
			($imageData=@getimagesize($sourceFile))
		) {

I’d be interested in hearing what you mean by “netbeans agrees” on using assignment inside a value check – since usually I don’t trust ‘tools’ to make those types of determinations and processing the result of an assignment is one of the REASONS for PHP functions to return a proper value OR false… Sounds like some C or Java programmer making their software ***** about things they don’t understand…

Well actually, that doesn’t make sense either – maybe it falls into the same category as these script babies nowadays who say similar things about using ‘with’?

But let’s face it, my stance on letting “tools” tell you how to code is quite well documented at this point.

It’s funny, because I saw that a long time ago in the Manual, but didn’t connect the dots. (I had added “XXXXX” to “00.txt” so I figured that was good enough, plus my “MyDocument.doc” was blank, although in retrospect, larger than 4K or whatever.)

I just added in a few rows of “XXXXXXXXXXXXX” to my “00.txt” and problem solved?!

Looks like I’ll add in something like this for safety…


if (filesize($tempFile)<4){
	echo "File Size Too Small";
	exit();
}

So… your original approach of supressing the error with @ and then using what I’m saying for checking for false early should handle that… or you could do a filesize check.

If I do that then I could remove the @ as far as this point goes.

if ($imageData=@getimagesize($tempFile)) {

since that still returns false on error, the error supression isn’t that bad a thing – unlike what you had where you weren’t trapping the result and just started working with the values…

True.

Probably why mine reads:


		if (
			file_exists($sourceFile) &&
			($imageData=@getimagesize($sourceFile))
		) {

Except this isn’t an issue of the file “existing”, it was an issue of the file “size”!

I’d be interested in hearing what you mean by “netbeans agrees” on using assignment inside a value check – since usually I don’t trust ‘tools’ to make those types of determinations and processing the result of an assignment is one of the REASONS for PHP functions to return a proper value OR false… Sounds like some C or Java programmer making their software ***** about things they don’t understand…

It has a yellow icon that says “Possible accidental assignment, assignments in conditions should be avoided” in the popup bubble.

Well actually, that doesn’t make sense either – maybe it falls into the same category as these script babies nowadays who say similar things about using ‘with’? But let’s face it, my stance on letting “tools” tell you how to code is quite well documented at this point.

I know.

So anyways, it looks like I fixed this bug that brought me to a grinding halt for the last two days?!

I’ll consult what you and others have written as far as other code, but I hope to keep to my style this go around.

Hopefully the tougher part is solved now?!

Thanks for helping,

Debbie

More I think on it the more I’d kill error reporting with @ and just use the ‘false’ trap to handle it being in error… because most of the time the condition should be true; at which point why run that check EVERY time. You don’t add more code for a ‘rare condition’ that can already be handled by the existing function status.

I was referring to the presence of the @, not the file_exists…

Wow, that’s really stupid – throwing a warning on a legitimate construct and the ENTIRE REASON C syntax languages have a distinction between = and ==, because it thinks it’s a typo. I guess there’s a reason if stuck in one of those garbage type editors I turn that bekaptah gobbledegook off. Put this alongside that illegible color coding garbage… actually no, file this alongside idiocy like ‘code completion’ that usually results in me having to spend more time correcting what the editor THINKS I want… actually no… it’s worse than any of that. That type of ‘warning’ makes me immediately think of clippy.

Probably why I use a flat text editor and test in XAMPP.

Hopefully so. Take a good hard look at the procedural version I posted since basically it provides everything you’ve been asking about from accessing the file type using getimagesize, loading and resizing the image, providing security (since it saves a resized/resampled copy instead of just copying it, so you KNOW the resulting file is an image) – it’s all there.