图像将不会显示,但会上传到服务器确定

时间:2015-11-16 20:47:25

标签: php html forms mysqli

我想使用上传功能允许用户上传个人资料照片。上传部分工作正常...但是,当我尝试显示下面上传的图像时,它显示为图像损坏。我已经尝试了很多不同的示例代码来解决这里已有的问题,而且我一遍又一遍地遇到同样的问题。任何帮助,将不胜感激!如果我的格式是垃圾,我会道歉......我很新。

这是我的upload.php

<?php require 'config/init.php';
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000000000000000000000)
&& in_array($extension, $allowedExts))
  {
if ($_FILES["file"]["error"] > 0)
  {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
  }
 else
  {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

   if     
(file_exists("/images/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
   else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"], "/images/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"]."<br>";

$image = $_FILES["file"]["name"]; 
$img = "/images/".$image;
echo "<img src=\"/images/$img\">";
  }
  }
 }
else
{
  echo "Invalid file";
}


?>

这是我的html表单

 <form action="upload.php" method="post" enctype="multipart/form-data">
 <label for="file">Filename:</label>
 <input type="file" name="file" id="file" /> 
 <br />
 <input type="submit" name="submit" value="Submit" />
 </form>

2 个答案:

答案 0 :(得分:1)

您正在回复HTML标记图片标记,如下所示:

<img src="/customers/2/e/e/jessicarosedavidson.co.uk//httpd.www/AIDAc3410801/assets/images/SOME_IMAGE.JPG">

这不是客户端浏览器的有效网址。您需要返回由您的服务器提供的对该图像有效的URL。

编辑:实际上,情况要糟糕得多,因为您使用相同的路径定义$img,然后再次使用路径预先挂起,这样您的网址就像:

<img src="/customers/2/e/e/jessicarosedavidson.co.uk//httpd.www/AIDAc3410801/assets/images//customers/2/e/e/jessicarosedavidson.co.uk//httpd.www/AIDAc3410801/assets/images/SOME_IMAG.JPG">

检查输出到浏览器。

答案 1 :(得分:1)

正如Hamish在另一个答案中指出的那样,您将本地文件路径与远程URL混淆。虽然PHP可以很好地处理硬盘驱动器上的文件,但您显然不希望将其暴露给外部世界。因此,您依靠Apache将某些文件路径转换为URL。根据您上面的评论,我认为这应该可以解决问题。请注意本地文件路径($file_location)与URL($image_url)的占位符变量。)

<?php
require 'config/init.php';
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 20000000000000000000000)
    && in_array($extension, $allowedExts)
) {
    if ($_FILES["file"]["error"] > 0) {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    } else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
        $local_file = "/customers/2/e/e/jessicarosedavidson.co.uk//httpd.www/AIDAc3410801/assets/images/" . $_FILES["file"]["name"];
        $image_url = "/AIDAc3410801/assets/images/" . $_FILES["file"]["name"];

        if (file_exists($local_file)) {
            echo $_FILES["file"]["name"] . " already exists. ";
        } else {
            move_uploaded_file($_FILES["file"]["tmp_name"], $local_file);
            echo "Stored in: " . $local_file ."<br>";
            echo "<img src=\"$image_url\">";
        }
    }
} else {
  echo "Invalid file";
}
?>