getimagesize() returns false on some images
After looking around SO I've found this insightful answer regarding PHP's upload limits. I combined it with what I already knew and finally got it working using the following code in my .htaccess (limiting uploads to 5MB):
php_value upload_max_filesize 5M
php_value post_max_size 8M
php_value max_input_time 1800
LimitRequestBody 0
Bottom line is: the problem wasn't in PHP's getimagesize()
function, it was that the images were never uploaded to the server due to file size limits in both Apache and PHP. Thanks everyone who answered and commented, It would have been a lot more painful to figure it out without your help!
getimagesize in php returning false
You can get file size directly using $_FILES['photo']['size']
or if it is image size like length and width of image you may have to check if GD package is loaded properly
PHP getimagesize() return value and errors
Here is the code
Reference http://php.net/manual/en/function.getimagesize.php from user contribution james dot relyea at zifiniti dot com
<?php
// Retrieve JPEG width and height without downloading/reading entire image.
function getjpegsize($img_loc) {
$handle = fopen($img_loc, "rb") or die("Invalid file stream.");
$new_block = NULL;
if(!feof($handle)) {
$new_block = fread($handle, 32);
$i = 0;
if($new_block[$i]=="\xFF" && $new_block[$i+1]=="\xD8" && $new_block[$i+2]=="\xFF" && $new_block[$i+3]=="\xE0") {
$i += 4;
if($new_block[$i+2]=="\x4A" && $new_block[$i+3]=="\x46" && $new_block[$i+4]=="\x49" && $new_block[$i+5]=="\x46" && $new_block[$i+6]=="\x00") {
// Read block size and skip ahead to begin cycling through blocks in search of SOF marker
$block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
$block_size = hexdec($block_size[1]);
while(!feof($handle)) {
$i += $block_size;
$new_block .= fread($handle, $block_size);
if($new_block[$i]=="\xFF") {
// New block detected, check for SOF marker
$sof_marker = array("\xC0", "\xC1", "\xC2", "\xC3", "\xC5", "\xC6", "\xC7", "\xC8", "\xC9", "\xCA", "\xCB", "\xCD", "\xCE", "\xCF");
if(in_array($new_block[$i+1], $sof_marker)) {
// SOF marker detected. Width and height information is contained in bytes 4-7 after this byte.
$size_data = $new_block[$i+2] . $new_block[$i+3] . $new_block[$i+4] . $new_block[$i+5] . $new_block[$i+6] . $new_block[$i+7] . $new_block[$i+8];
$unpacked = unpack("H*", $size_data);
$unpacked = $unpacked[1];
$height = hexdec($unpacked[6] . $unpacked[7] . $unpacked[8] . $unpacked[9]);
$width = hexdec($unpacked[10] . $unpacked[11] . $unpacked[12] . $unpacked[13]);
return array($width, $height);
} else {
// Skip block marker and read block size
$i += 2;
$block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
$block_size = hexdec($block_size[1]);
}
} else {
return FALSE;
}
}
}
}
}
return FALSE;
}
?>
Related Topics
PHP MySQL SQL Parser (Insert and Update)
Call PHP from JavaScript and Return an Array from PHP to JavaScript Function
Warning: File_Get_Contents: Failed to Open Stream: Redirection Limit Reached, Aborting
How to Get the Last Dir from a Path in a String
How to Solve "Non-Static Method Xxx:Xxx() Should Not Be Called Statically in PHP 5.4
Do I Need to Escape My Variables If I Use MySQLi Prepared Statements
How to Get the List of Available Locales in PHP
How to Integrate PHPmailer with Codeigniter 3
How to Disable PHP Magic Quotes at Runtime
Php: Populating an Array with the Names of the Next 12 Months
SQL Query with Binary Data (PHP and MySQL)
Are There Different Types of Double Quotes in Utf-8 (Php, Str_Replace)
Extract Dom-Elements from String, in PHP
Calling PHP Function from Jquery
Using PHP Include to Separate Site Content