Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is generally a bad habit to check file type using the extension. I can easily create a text file and rename it .jpg According to this code it would suddenly become an actual image. Or even more evil: I might exploit some bug in webbrowsers by creating a fake jpg that actually executes some code.</p> <p>Besides that, I might name my jpeg files JPEG, jPG, jpG, JPeg or any other combination you might think of. Then there's also other formats than jpeg. I don't know the rest of your code, but maybe you want someone to allow uploading png or gif images.</p> <p>If you want to be a little more sure that you actually have an image file, a more sure way to check is using <a href="http://php.net/getimagesize" rel="nofollow noreferrer">getimagesize</a>. That does require the GD extension though. If that function provides you with output that seems likely to be real you're a whole lot safer using that file as an image. Sure there are cases when you don't care and just want to check for the extension of a file. But I get a feeling this code is supposed to get a little public. A little example:</p> <pre><code>&lt;?php $file = $row['dfilepath']; // where upload_dir is a function that somehow finds // the absolute path of the upload dir $path = upload_dir() . DIRECTORY_SEPARATOR . $file; $valid = array('image/jpeg', 'image/gif', 'image/png'); $info = getimagesize($path); if($info !== false &amp;&amp; in_array($info['mime'], $valid) { // more likely you'll actually have an image here // now you might want to print some html here, like: ?&gt;&lt;img src="/uploads/&lt;?php print $file; ?&gt;" alt="" /&gt;&lt;?php } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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