Actually, I think I've figured it out now after playing around a bit.
I went along the same principal as you suggested, so that the script doesn't exit just because there is no image. I looked at how the script was originally, before I added bits and pieces with the help of this forum to limit the file type and size that could be uploaded. It basically said:
if ( is_uploaded_file($_FILES['userfile']['tmp_name']) ) {
$fileName = $_FILES['userfile']['tmp_name'];
$fileHandle = fopen($fileName, 'r');
$fileAttach = fread($fileHandle, filesize ($fileName));
fclose($fileHandle);
$fileAttach = chunk_split(base64_encode($fileAttach));
$emailBody .= "--FTG_BOUNDRY\n"
. "Content-Type: " . $_FILES['userfile']['type'] . "; name=\"" . $_FILES['userfile']['name'] . "\"\n"
. "Content-disposition: attachment\n"
. "Content-transfer-encoding: base64\n"
. "\n"
. "$fileAttach\n"
. "\n";
}
Of course the way I had edited this for security meant that the script exited instead of just carrying on if there was no file uploaded. It's really all my own fault because I entered the exit & redirect bit without guidance to let the user know they had done wrong, thinking I was being clever!
With this new bit I simply re-entered the original if statement and put my new one withinin it to carry out further checks before proceeding....
$allowed_types = array( // List all allowed MIME Types here
'image/gif',
'image/pjpeg',
'image/jpeg',
'image/jpg',
'image/tiff',
'image/bmp',
'image/png',
);
$extArray = array('jpg', 'jpeg', 'jpe', 'gif', 'tif', 'tiff', 'png', 'bmp');
if( is_uploaded_file($_FILES['userfile']['tmp_name']) )
{
if ( is_uploaded_file($_FILES['userfile']['tmp_name']) && in_array($_FILES['userfile']['type'], $allowed_types) && in_array(substr($_FILES['userfile']['name'], strrpos($_FILES['userfile']['name'], '.')+1), $extArray )) {
$fileName = $_FILES['userfile']['tmp_name'];
$fileHandle = fopen($fileName, 'r');
$fileAttach = fread($fileHandle, filesize ($fileName));
fclose($fileHandle);
$fileAttach = chunk_split(base64_encode($fileAttach));
$emailBody .= "--FTG_BOUNDRY\n"
. "Content-Type: " . $_FILES['userfile']['type'] . "; name=\"" . $_FILES['userfile']['name'] . "\"\n"
. "Content-disposition: attachment\n"
. "Content-transfer-encoding: base64\n"
. "\n"
. "$fileAttach\n"
. "\n";
} else {
header("Location: http://www.kraftworkvehiclerefinishing.com/forbidden.html");
exit;
}
}
...well, it works but I would like your advice on whether I have made the script too messy or made the script unsecure again as far as image uploading goes.