Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>edit</h2> <p>I just realized that you want to allow PDF files as well. In that case check out <a href="http://us3.php.net/manual/en/ref.fileinfo.php" rel="noreferrer">PHP's Fileinfo class and functions</a>. But as far as security goes, you still shouldn't rely on <code>$_FILES[]['type']</code> :)</p> <p>I'll leave the rest here in case it helps someone else who finds this question</p> <hr> <p>For checking the mime type of the image, <code>$_FILES[]['type']</code> could be unsafe. This data is sent by the browser and could be easily spoofed.</p> <p>You should use the <code>getimagesize()</code> function if you only want to allow images to be uploaded (despite its maybe misleading name). This function won't just give you the size but all the data you will probably need about the image.</p> <p>I used the following script in an image handling class:</p> <pre><code>private function load_image_data($image_file) { // Firstly, to disambiguate a loading error with a nonexistant file error, // check to see if the file actually exists. if( ! file_exists($image_file) ) { throw new Nonexistent_Image_Exception("The file '{$image_file}' does not exist"); } // We're going to check the return value of getimagesize, so we don't // need any pesky warnings or notices popping up, since we're going to // stop execution of this function if something goes wrong. $image_data = @getimagesize($image_file); if( $image_data === false ) { throw new Load_Image_Exception("Could not get image data from '{$image_file}'"); } $this-&gt;size = new Dimensions($image_data[0], $image_data[1]); $this-&gt;mime = $image_data['mime']; } </code></pre> <p>Notice that <code>getimagesize()</code> returns an associative array containing a 'mime' index. The data here is reliable.</p> <p>In another function I checked the mime type of the image and converted it to PNG with the appropriate GD function:</p> <pre><code>private function load_image($image_file) { // Suppress warning messages because we're going to throw an // exception if it didn't work instead. switch( $this-&gt;mime ) { case 'image/jpeg': case 'image/pjpeg': $this-&gt;image = @imagecreatefromjpeg($image_file); break; case 'image/gif': $this-&gt;image = @imagecreatefromgif($image_file); break; case 'image/png': $this-&gt;image = @imagecreatefrompng($image_file); break; default: throw new Invalid_Image_Exception("The image was of an invalid type"); } if( $this-&gt;image === false ) { throw new Load_Image_Exception("Loading of image '{$image_file}' failed"); } } </code></pre> <p>You probably won't need to do all of this, but you can see what mime types appear for the filetypes you have specified. Notice that a jpeg could have two different mime types.</p> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload