@
@
@atambalasi2016-02-23 18:45:18
PHP
@atambalasi, 2016-02-23 18:45:18

Is there a way to check the image size before uploading to the server?

There is an API server on nginx (ubuntu) with php. Is it possible to check the image size before uploading? Or if the image size is more than 2mb then send the image size to php script. The server has a 2MB limit. Now if the user tries to upload more than 2mb, it gives an error 413 Request Entity Too Large. Can I somehow get (catch) this error in PHP script? Do not issue a 413 error, but simply return to the client for example.
{'size_error':true}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Igor, 2016-02-23
@unitby

stackoverflow.com/questions/20137234/how-to-check-...

O
OVK2015, 2016-02-23
@OVK2015

function getFileSize($url)
    {
      $cURLsession = curl_init();
      curl_setopt($cURLsession, CURLOPT_URL, $url);
      curl_setopt($cURLsession, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt($cURLsession, CURLOPT_HEADER, TRUE);
      curl_setopt($cURLsession, CURLOPT_NOBODY, TRUE);

      $data = curl_exec($cURLsession);
      if(($curlResult = curl_exec($cURLsession)) === false)		
      {	
        echo "Ошибка при получении данных: ".curl_error($cURLsession).". С ".$url;				
      }
      $responseType = curl_getinfo($cURLsession, CURLINFO_CONTENT_TYPE);		
      $fileSize = curl_getinfo($cURLsession, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    

      curl_close($cURLsession);	

      return  $fileSize;						
    }

If more than allowed, we will not ship

K
kiloper, 2016-02-25
@kiloper

Send by Ajax, if it returns an error 413, then give a message to the user that the file size has been exceeded.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question