图像已成功上传到数据库并显示在目录中,但不会显示在 php 页面上
Images are successfully uploaded to database and display in directory but it won't display on php page
图片已成功上传到数据库并显示在目录中,但不会显示在php页面上。图片在 php 页面上显示为损坏的图片。帮我让它们显示在 php 页面上。我按照一个教程看了很多遍,以确保没有导致图像损坏的错误。几天来一直在尝试修复它,但它不会显示图像。我在 Mac OS 上使用 XAMPP。请查看我的代码并告诉我为什么它不会在 php.
上显示图像
marketplace_upload.inc.php
从这里开始:
if (isset($_POST['submit'])) {
$newFileName = $_POST['filename'];
if (empty($newFileName)) {
$newFileName = "gallery";
} else {
$newFileName = strtolower(str_replace(" ", "-", $newFileName));
}
$imageTitle = $_POST['filetitle'];
$imagePrice = $_POST['fileprice'];
$file = $_FILES['file'];
$fileName = $file["name"];
$fileType = $file["type"];
$fileTempName = $file["tmp_name"];
$fileError = $file["error"];
$fileSize = $file["size"];
$fileExt = explode(".", $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array("jpg", "jpeg", "png");
if (in_array($fileActualExt, $allowed)) {
if ($fileError === 0) {
if ($fileSize < 2000000) {
$imageFullName = $newFileName . "." . uniqid("", true) . "." . $fileActualExt;
$fileDestination = "../img/gallery/" . $imageFullName;
include_once "dbh.inc.php";
if (empty($imageTitle) || empty($imagePrice)) {
header("Location:../gallery.php?upload=empty");
exit();
} else {
$sql = "SELECT * FROM gallery;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL statement failed!";
} else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$rowCount = mysqli_num_rows($result);
$setImageOrder = $rowCount + 1;
$sql = "INSERT INTO gallery (titleGallery, priceGallery, imgFullNameGallery, orderGallery) VALUES (?, ?, ?, ?);";
}
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL statement failed!";
} else {
mysqli_stmt_bind_param($stmt, "ssss", $imageTitle, $imagePrice, $imageFullName, $setImageOrder);
mysqli_stmt_execute($stmt);
move_uploaded_file($fileTempName, $fileDestination);
header("Location: ../gallery.php?upload=success");
}
}
}
} else {
echo "File size is too big!";
exit();
}
} else {
echo "You had an error!";
exit();
}
} else {
echo "You need to upload a proper file type!";
exit();
}
Screenshot of gallery.php codes where images are supposed to be displayed. It won't let me paste the codes here.
我希望在我的上传网站上上传的所有图片也能显示在本地主机中。
感谢所有帮助过我或试图帮助过我的人。我终于解决了这个问题。我的图片没有显示在 php 页面上的原因是我没有更改权限。一旦我更改了读写代码的权限,我就开始做我想让他们做的事。
图片已成功上传到数据库并显示在目录中,但不会显示在php页面上。图片在 php 页面上显示为损坏的图片。帮我让它们显示在 php 页面上。我按照一个教程看了很多遍,以确保没有导致图像损坏的错误。几天来一直在尝试修复它,但它不会显示图像。我在 Mac OS 上使用 XAMPP。请查看我的代码并告诉我为什么它不会在 php.
上显示图像marketplace_upload.inc.php
从这里开始:
if (isset($_POST['submit'])) {
$newFileName = $_POST['filename'];
if (empty($newFileName)) {
$newFileName = "gallery";
} else {
$newFileName = strtolower(str_replace(" ", "-", $newFileName));
}
$imageTitle = $_POST['filetitle'];
$imagePrice = $_POST['fileprice'];
$file = $_FILES['file'];
$fileName = $file["name"];
$fileType = $file["type"];
$fileTempName = $file["tmp_name"];
$fileError = $file["error"];
$fileSize = $file["size"];
$fileExt = explode(".", $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array("jpg", "jpeg", "png");
if (in_array($fileActualExt, $allowed)) {
if ($fileError === 0) {
if ($fileSize < 2000000) {
$imageFullName = $newFileName . "." . uniqid("", true) . "." . $fileActualExt;
$fileDestination = "../img/gallery/" . $imageFullName;
include_once "dbh.inc.php";
if (empty($imageTitle) || empty($imagePrice)) {
header("Location:../gallery.php?upload=empty");
exit();
} else {
$sql = "SELECT * FROM gallery;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL statement failed!";
} else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$rowCount = mysqli_num_rows($result);
$setImageOrder = $rowCount + 1;
$sql = "INSERT INTO gallery (titleGallery, priceGallery, imgFullNameGallery, orderGallery) VALUES (?, ?, ?, ?);";
}
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL statement failed!";
} else {
mysqli_stmt_bind_param($stmt, "ssss", $imageTitle, $imagePrice, $imageFullName, $setImageOrder);
mysqli_stmt_execute($stmt);
move_uploaded_file($fileTempName, $fileDestination);
header("Location: ../gallery.php?upload=success");
}
}
}
} else {
echo "File size is too big!";
exit();
}
} else {
echo "You had an error!";
exit();
}
} else {
echo "You need to upload a proper file type!";
exit();
}
Screenshot of gallery.php codes where images are supposed to be displayed. It won't let me paste the codes here.
我希望在我的上传网站上上传的所有图片也能显示在本地主机中。
感谢所有帮助过我或试图帮助过我的人。我终于解决了这个问题。我的图片没有显示在 php 页面上的原因是我没有更改权限。一旦我更改了读写代码的权限,我就开始做我想让他们做的事。