使用WAMP的PHP脚本显示带有标签的HTML输出

时间:2013-02-06 16:34:02

标签: php wamp

enter code here这是我关于Stack Overflow的第一篇文章,所以请原谅我,如果我太模糊,并且没有给你必要的信息来立即诊断我的问题!

我创建了一个使用表单上传图片的临时页面。然后该表单发布到一个php页面,该页面重新采样图像并创建一个新图像,调整大小并裁剪。

我遇到的问题是,在图像上传到的php页面上,所有PHP代码都正常发布并按照我的预期运行,但是所有HTML代码都显示在浏览器中而没有格式化。我不知道为什么,我以前从未遇到过这个问题。

以下是上传表单页面的代码:

<! Image test: Testing the ability to upload an image, resize and crop. >
<! Idealy, this procedure would be performed on large images. >

<! Car Listing page images: 280 x 200 px >

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <?php require_once("baseopen.php"); ?>
   <?php

   if (isset($_POST['error1'])) {
    $error = "The file selected was not of the correct filetype.<br />
     Please select a different file to upload.";
   }

   ?>

   <html>
   <?php if (isset($error)) { echo $error; } ?>
   <p>Select an image of type <b>.JPG, .GIF, .PNG, or .BMP</b> to upload.</p><br />
   <form action="converter.php" method="post" enctype="multipart/form-data">
   <label for="image">Picture:</label>
   <input type="file" name="image"><br />
   <input type="submit" name="submit" value="Upload">
   </form>

   </html>

   <?php mysql_close($data); ?>

以下是我处理PHP的问题,它提供了问题并输出了所有HTML标记。 (请注意,PHP已处理,未在浏览器中显示。)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<?php
 if($_SERVER['REQUEST_METHOD'] == "POST") {
  $file = $_FILES['image']['name'];
  $servfile = $_FILES['image']['tmp_name'];
  echo "\$File: " . $file . "<br />";
  echo "\$servfile: " . $servfile . "<br /><br />";

  $filename = explode(".", $file);
  echo $filename[0] . "<br />";
  echo $filename[1] . "<br />";
  $filename[1] = strtolower($filename[1]);

  /* Begin execution once file extension is confirmed. */
  if ($filename[1] == "jpg" || $filename[1] == "gif" || $filename[1] == "png" || $filename[1] == "bmp") {
   echo "Successful recognition! <br />";
   crop($servfile, $filename[1], "280", "200");
  }
  /* Send back to main page with an error code. */
  else {
   echo "Error, dude!";
  }
 }

 function crop($filename, $type, $width, $height) {
  /* Create a new image from the proper filetype. */
  if ($type == "jpg") { $resource = imagecreatefromjpeg($filename); }
  if ($type == "gif") { $resource = imagecreatefromgif($filename); }
  if ($type == "png") { $resource = imagecreatefrompng($filename); }
  if ($type == "bmp") { $resource = imagecreatefrombmp($filename); }

  echo "Width is: " . imagesx($resource) . "<br />";
  echo "Height is: " . imagesy($resource) . "<br />";
  echo "Desired width is: " . $width . "<br />";
  echo "Desired height is: " . $height . "<br />";

  /* Check to make sure image is larger than needed size. */
  if ( ($width < imagesx($resource)) && ($height < imagesy($resource))) {
   echo "We can do this! <br />";


   $new = imagecreatetruecolor($width, $height);
   imagecopyresampled($new, $resource,
    0, 0, 0, 0, $width, $height, imagesx($resource), imagesy($resource));
    header('Content-type: image/jpeg');
    imagejpeg($new, "temp.jpg", 100);
    echo "<img src=\"temp.jpg\">";
  }
  /* Return with error that image is too small! */
  else { echo "Nope, not big enough."; }
  }
?>
</html>

目前正在使用运行PHP 5.3.5的WAMPSERVER。

测试服务器运行所有PHP页面没有任何错误,除了这个我几个星期以来一直在争夺的页面。

ADDED:当尝试上传名为“8.jpg”的图像时,Internet Explorer中的输出就是这个。这是converter.php输出的实际视图:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html> $File: 8.jpg<br />$servfile: C:\wamp\tmp\php82F0.tmp<br /><br />8<br />jpg<br />Successful recognition! <br />Width is: 1680<br />Height is: 1050<br />Desired width is: 280<br />Desired height is: 200<br />We can do this! <br /><img src="temp.jpg"></html>

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

提交图片进行上传时似乎存在问题。这可能是由表单中的enctype引起的,也可能是由 converter.php 引起的,它为 image / jpeg mime_type 创建了标题。

无论哪种方式,尝试多个浏览器后代码都无法正常工作,Chrome会将页面显示为损坏的图像。

我解析了代码并使 converter.php 成为一个带有 no 输出的独占处理脚本。然后,此脚本重定向到一个新的PHP页面,我将其命名为 ServedUp.php 。使用新调整大小的图像正确显示结果。

我不确定这个冲突是在脚本中创建的,还是在服务器端的翻译中创建的。不用说,问题现在已经解决了。

感谢大家的想法和意见。当另一个问题上升时,我将很快再次使用Stack Overflow!