Hack Forums
[PHP] PHP Image Uploader - Add another extension - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] PHP Image Uploader - Add another extension (/showthread.php?tid=5123518)



PHP Image Uploader - Add another extension - Exodi - Jan 6, 2016

So I have a PHP file uploader to upload files with a watermark on it.
It totally works and it's a great script.
But it only allows JPG and JPEG extensions, and I'd like to add PNG.

I already tried this:
PHP Code

<?php 
if ($_POST['submit'] && $_FILES['userfile']['size'] <= $maxkb && !file_exists($path . $_FILES['userfile']['name']) && $_FILES['userfile']['size'] && is_uploaded_file($_FILES['userfile']['tmp_name']) && (strtolower(substr($_FILES['userfile']['name'], -3)) == "jpg" || strtolower(substr($_FILES['userfile']['name'], -4)) == "png")) 
but that doesn't work. (only the JPG and JPEG works, but I tried to add PNG but only that doesn't work)

FULL PHP CODE: http://pastebin.com/QZeiqMVw
it was too large to post on HF

Tl;dr: I need PNG to be allowed as file extension in a PHP script


* - ๖ۣۜNyanbot - Jan 6, 2016

Code
if ($_POST['submit'] && $_FILES['userfile']['size'] <= $maxkb && !file_exists($path . $_FILES['userfile']['name']) && $_FILES['userfile']['size'] && is_uploaded_file($_FILES['userfile']['tmp_name']) && (strtolower(substr($_FILES['userfile']['name'], -3)) == "jpg" || strtolower(substr($_FILES['userfile']['name'], -3)) == "png"))

The substr() for PNG only needs to be set to -3.

Having it set to -4 means it goes back 4 characters and would be capturing ".png", so it would return false as you are comparing it to "png".


* - stackoverflowin - Jan 6, 2016

Bit of a stupid way when you can just get the MIME type of the file. Ref: http://php.net/manual/en/function.finfo-file.php