I getimagesize() uploaded by users with getimagesize() This is how:

 $imageinfo = getimagesize($file['tmp_name']); if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/png') { $data['error'] = 'Ошибка Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ. ДопустимыС Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Ρ‹: .jpeg .gif .png'; echo json_encode( $data ); exit; } else { list($width, $height) = getimagesize($file['tmp_name']); if ($width < $minWidth || $height < $minHeight || $height > $maxHeight || $width > $maxWidth){ $data['error'] = 'Π˜Π·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ мСньшС '.$minWidth.' ΠΈ большС '.$maxWidth.' пиксСлСй <br> ΠΏΠΎ ΠΎΠ΄Π½ΠΎΠΉ ΠΈΠ· сторон. ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π° Π²Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ Π΄Ρ€ΡƒΠ³ΠΎΠ΅ Ρ„ΠΎΡ‚ΠΎ.'; echo json_encode( $data ); exit; } } 

That is a check for mime types. Everything works perfectly with different formats, but when users load a .txt file instead of images, the server issues 500 (Internal Server Error) with the words LOG.error: exception 'ErrorException' with message 'getimagesize(): Read error!

Tried to do so:

 try {getimagesize($file['tmp_name']);} catch (Exception $e) { } if ($e === FALSE) { //ERROR } 

Did not help. How to do it so that in case of an error of execution of getimagesize () there was not a 500 error, but a response in the form echo json_encode( $error ); ?

  • And, can knowledgeable people tell ... What is so special about the .txt format ?? Just getimagesize () "wrong" behaves only with txt files! - Daddymanphp

1 answer 1

Try using the @ operator :

 if ($imageinfo = @getimagesize($file['tmp_name'])) { //...ваш ΠΊΠΎΠ΄ } else { //Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ json с ошибкой } 
  • Thank you, as he did not guess! But there is a problem. I use Blade template engine and laravel frame. I hope there will be no conflicts with the operators ... I'll try to accomplish my goal! - Daddymanphp
  • There are no conflicts with the blade! Everything works fine, thanks! - Daddymanphp