I use the Fox Contact component on my website, and through that component users send me photos (as an attachment) I had to make a few modifications in the upload form itself and make filters for file name (only certain names are allowed). This filter works without any problems. I still have to do validation to check the size of the photo but I have no success. Even though the photo is larger than 1000px I don’t get a warning. This is my code:
//Name validation (this is OK)
$realname = $this->getFileName();
$realname = Normalizer::normalize($realname);
setlocale(LC_ALL,'bs_BA.UTF-8');
$onlyname = pathinfo($realname, PATHINFO_FILENAME);
//Create list of all names
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('eio3k_peaks.peak_name');
$query->from($db->quoteName('#__peaks'));
$db->setQuery($query);
$peaks_list = $db->loadColumn();
if (!in_array($onlyname, $peaks_list))
{
return array('error' => JText::_('The name of this photo "' .$onlyname. '" is incorrect.'));
}
//Size validation (this is not ok)
$dir = 'https://mysite.com/uploads/';
$image_filename = $dir . $realname;
list($width, $height) = getimagesize($image_filename);
if (($width > 1000) OR ($height > 1000))
{
return array('error' => JText::_('The photo is larger than 1000px'));
}
Where I went wrong? This is just the code I added. I can set up a complete file, but it's more than 250 lines of code
eio3k_peaks.
is unnecessary. In fact, you can refine your error checking process, by applying thein_array()
logic into your query as awhere()
condition -- this way you can return a count and just check ifloadResult()
is truthy/falsey. As for the second half of that snippet, can you verify that the$image_filename
value is as expected? Are$width
and$height
as expected? I don't recommend theOR
operator, I would always use||
for consistency. Or skip thelist()
and useif(max(getimagesize($image_filename)) > 1000)
– mickmackusa♦ Jun 20 '20 at 21:27if(max(getimagesize($image_filename)) > 1000)
? In any case, the result is the same for me – Midhat Jun 21 '20 at 9:52getimagesize()
:Returns an array with up to 7 elements.
, which means there may be additional elements that will have a higher value that I am not expecting to see. For reliability, the array elements would need to be truncated byarray_slice()
. ideone.com/uefwuk – mickmackusa♦ Jun 21 '20 at 12:22getimagesize()
when it fails to work as expected. This should make your question complete and satisfy my requirements to upvote the question. – mickmackusa♦ Jun 21 '20 at 12:26