I was trying to get image file from user by using HTML form and PHP, all good but when the request is sent to the server the client gets an http 500 Error. I guess this related to some code issues but I couldn't find it. PHP storm debugger does not show any errors
my html form:
<form method="POST" action="img.php" enctype="multipart/form-data">
<textarea style="resize:none" name="data"></textarea>
<div> <input type="file" name="upfile" id="upfile" /></div>
<input name="submit" type="submit" value="encrypt" />
<input name="submit" type="submit" value="decrypt" />
</form>
my php code:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$file = $_FILES['upfile']['tmp_name'];
if (file_exists($file)) {
$imagesizedata = getimagesize($file);
if ($imagesizedata === FALSE || !testExtension($_FILES['upfile']['name']) || ($_FILES['upfile']['size'] > 50000)) {
throw new Exception("The file is not an image Or to big");
} else {
$img = imagecreatefromjpeg($file);
$data = inputValidation($_POST['data']); //example
$selection = inputValidation($_POST['submit']);
if ($selection == 'encrypt') { //image
if ($q = strlen($data) % 3 != 0) {
$q==1?$data.="--":$data.="-";
}
if ($img != false) {
$j = 0;
for ($i = 0; $i <= (strlen($data) - 3); $i += 3) {
$part = substr($data, $i, 3);
$color = getEncryptedColor($img, $part);
imagesetpixel($img, $j++, 0, $color);
}
}
//display image//+*+*+*+*+*+*+*
header('Content-Type: image/jpeg');
imagejpeg($img);
} else if ($selection == 'decrypt') {
//decrypt from image - remove on release
$dataDec = decryptAllData($img, strlen($data) / 3);
echo "".$dataDec;
}
}
} else {
throw new Exception("The upload is not a file or the file doesn't exist anymore.");
}
error_reporting(E_ALL);
ini_set('display_errors', 1);
}
function inputValidation($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function decryptAllData($img, $datasize) {
$decrypted = "";
for ($i = 0; $i < $datasize; $i++) {
$decrypted.= decryptDataFromPixel($img, $i, 0);
}
return $decrypted;
}
function getEncryptedColor($img, $string) {
return imagecolorallocate($img, ord($string[0]), ord($string[1]), ord($string[2]));
}
function printImageValues($image, $num) {
for ($x = 0; $x < $num; $x++) {
echo "\n".imagecolorat($image, $x, $x);
}
echo "--END--";
}
function decryptDataFromPixel($img, $x, $y) {
$currpixel = imagecolorat($img, $x, $y);
$colors = imagecolorsforindex($img, $currpixel);
$str = "".chr($colors["red"]).chr($colors["green"]).chr($colors["blue"]);
return $str;
}
function testExtension($current_image) {
$extension = substr(strrchr($current_image, '.'), 1);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "gif") && ($extension != "png") && ($extension != "bmp")) {
return false;
}
return true;
}