我想使用上传工具来允许用户上传个人资料图片。上传部分工作正常...但是,当我尝试显示下面上传的图像时,它显示为损坏的图像。我已经尝试了这里已经存在的问题中的许多不同的示例代码,并且我一遍又一遍地遇到相同的问题。任何帮助,将不胜感激!如果我的格式很垃圾,请道歉...我很新
这是我的 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>
最佳答案
正如 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";
}
?>
关于php - 图片无法显示,但已上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33744296/