2017-09-30 81 views
-1

我试图通过使用HTML表单和PHP从用户获取图像文件,都很好,但是当请求发送到服务器时,客户端得到一个http 500错误。我想这涉及到一些代码问题,但我找不到它。 PHP风暴调试器不显示任何错误PHP http错误500,域无法处理此请求

我的HTML表单:

<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> 

我的PHP代码:

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; 
} 
+1

500可以表示任何错误,您可以切换显示错误吗?例如'$ data。 =“ - ”:$ data。 =“ - ”;'是不好的php代码 –

+0

@IvanBolnikh这是我的错,JS漂亮的打印弄乱了那些 – mplungjan

+0

你可能想要改变'$ q == 1?$ data。=“ - ”:$ data 。=“ - ”;'到'$ data。=($ q == 1)? “ - ”:“ - ”;' –

回答

0

请检查空间中三元运算符

$q == 1 ? $data.= "--" : $data.= "-"; 

$data.= ($q == 1 ? "--" : "-"); 
+0

这是一个格式问题。不是原来的原来是'$ q == 1?$ data。=“ - ”:$ data。=“ - ”;' – mplungjan

相关问题