0

因此,我查看了几乎所有问题,并尝试使用下面的 php 脚本显示我的图像。但它不起作用。我还尝试查看是否使用 PHPInfo() 启用了 GD 库并且它也可以正常工作。我对 PHP 很陌生,但似乎无法使其正常工作。谢谢你的帮助!

编辑:单击上传文档按钮后,我得到一个损坏的图像图标。

<?php
 if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_FILES['photo'])
        && is_uploaded_file($_FILES['photo']['tmp_name'])
        && $_FILES['photo']['error'] == UPLOAD_ERR_OK) {
            foreach ($_FILES['photo'] as $key => $value) {
                echo "$key : $value<br />";
            }
            if($_FILES['photo']['type'] == 'image/jpeg') {
                $tmp_img = $_FILES['photo']['tmp_name'];
                $image = imagecreatefromjpeg($tmp_img);
                header('Content-Type: image/jpeg');
                imagejpeg($image,NULL);
                imagedestroy($image);
            } else {
                echo "Uploaded file ewas not a jpg image.";
            }
                echo "no photo uploaded.";
              }
}
?>
<form enctype="multipart/form-data" action="book.php" method="post">
<input type="file" name="photo">
<input type="submit" value="upload a doc">
</form>
4

3 回答 3

0

您无法使用 直接访问图像tmp_name。首先,您应该将图像保存到服务器,然后您可以使用imagecreatefromjpeg

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_FILES['photo'])
        && is_uploaded_file($_FILES['photo']['tmp_name'])
        && $_FILES['photo']['error'] == UPLOAD_ERR_OK) {
            if($_FILES['photo']['type'] == 'image/jpeg') {
                $tmp_img = $_FILES['photo']['tmp_name'];
                $uniq_name = uniqid().".".explode(".",$tmp_img)[1];
                move_uploaded_file($_FILES['photo']['tmp_name'],$uniq_name);
                $image = imagecreatefromjpeg($uniq_name);
                header('Content-Type: image/jpeg');
                imagejpeg($image);
                imagedestroy($image);
            } else {
                echo "Uploaded file ewas not a jpg image.";
            }
                echo "no photo uploaded.";
              }
}
?>
<form enctype="multipart/form-data" action="book.php" method="post">
<input type="file" name="photo">
<input type="submit" value="upload a doc">
</form>
于 2013-10-13T17:28:30.767 回答
0

我认为问题在于echoPHP 部分(HTML 代码)之后的输出字符串。foreach仅当它是新页面或出现问题时才删除循环并输出 HTML。

<?php
  if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(isset($_FILES['photo'])
        && is_uploaded_file($_FILES['photo']['tmp_name'])
        && $_FILES['photo']['error'] == UPLOAD_ERR_OK) {
            if($_FILES['photo']['type'] == 'image/jpeg') {
                $tmp_img = $_FILES['photo']['tmp_name'];
                $image = imagecreatefromjpeg($tmp_img);
                header('Content-Type: image/jpeg');
                imagejpeg($image,NULL);
                imagedestroy($image);
                exit; // Add this to stop the program here.
            } else {
                echo "Uploaded file was not a jpg image.";
            }
    } else {
      echo 'No file was sent.';
    }
}
?>
<form enctype="multipart/form-data" action="book.php" method="post">
<input type="file" name="photo">
<input type="submit" value="upload a doc">
</form>

还有一件事,如果您使用记事本进行编码,请确保文件开头没有隐藏字符。

于 2013-10-13T17:29:09.630 回答
0

您可以尝试添加die();imagedestroy($image);以防止除图像数据之外的任何其他输出。也imagejpeg($image,NULL);改为imagejpeg($image);.

于 2013-10-13T17:12:26.200 回答