This question already has an answer here:
- How can I get useful error messages in PHP? 26 answers
- Move_uploaded_file() function is not working 11 answers
When I try to upload an image file, the url says success, but the image file is not uploaded to the webserver, and only the image title and descriptions are saved into the database. I will provide all the code that has to do with this photo gallery. I have provided a few screenshots to represent the issues I've been having. I also attached a screenshot of the directory that my webserver is operated from.
gallery_upload.inc.php
<?php
if (isset($_POST['submit'])) {
$newFileName = $_POST['filename'];
if (empty($newFileName)) {
$newFileName = "gallery";
} else {
$newFileName = strtolower(str_replace(" ", "-", $newFileName));
}
$imageTitle = $_POST['filetitle'];
$imageDesc = $_POST['filedesc'];
$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($imageDesc)) {
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, descGallery, imgFullNameGallery, orderGallery) VALUES (?, ?, ?, ?);";
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL statement failed!";
} else {
mysqli_stmt_bind_param($stmt, "ssss", $imageTitle, $imageDesc, $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();
}
}
dbh.inc.php
<?php
$servername = "localhost";
$username = "*******";
$password = "*******";
$dbname = "gallery";
$conn = mysqli_connect($servername, $username, $password, $dbname);
gallery.php
<?php include("includes/a_config.php");?>
<?php
$_SESSION['username'] = "Surnic";
?>
<!DOCTYPE html>
<html>
<head>
<?php include("includes/head-tag-contents.php");?>
<meta charset="utf-8">
<title>My Portfolio</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Catamaran:100,200,300,400,500,600,700,800,900|Cormorant+Garamond:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">
<link rel="stylesheet" href="style2.css">
</head>
<body>
<header>
<?php include("includes/navigation.php");?>
</header>
<main>
<section class="gallery-links">
<div class="wrapper">
<h2>Gallery</h2>
<div class="gallery-container">
<?php
include_once 'includes/dbh.inc.php';
$sql = "SELECT * FROM gallery ORDER BY orderGallery DESC;";
$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);
while ($row = mysqli_fetch_assoc($result)) {
echo '<a href="#">
<div style="background-image: url(img/gallery/'.$row["imgFullNameGallery"].');"></div>
<h3>'.$row["titleGallery"].'</h3>
<p>'.$row["descGallery"].'</p>
</a>';
}
}
?>
</div>
<?php
if (isset($_SESSION['username'])) {
echo '<div class="gallery-upload">
<h2>Upload</h2>
<form action="includes/gallery-upload.inc.php" method="post" enctype="multipart/form-data">
<input type="text" name="filename" placeholder="File name...">
<input type="text" name="filetitle" placeholder="Image title...">
<input type="text" name="filedesc" placeholder="Image description...">
<input type="file" name="file">
<button type="submit" name="submit">UPLOAD</button>
</form>
</div>';
}
?>
</div>
</section>
</main>
<div class="wrapper">
<footer>
<ul class="footer-links-main">
<li><a href="#">Home</a></li>
<li><a href="#">Cases</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">About me</a></li>
<li><a href="#">Contact</a></li>
</ul>
<ul class="footer-links-cases">
<li><p>Latest cases</p></li>
<li><a href="#">MALING SHAOLIN - WEB DEVELOPMENT</a></li>
<li><a href="#">EXCELLENTO - WEB DEVELOPMENT, SEO</a></li>
<li><a href="#">MMTUTS - YOUTUBE CHANNEL</a></li>
<li><a href="#">WELTEC - VIDEO PRODUCTION</a></li>
</ul>
<div class="footer-sm">
<a href="#">
<img src="/img/youtube-symbol.png" alt="youtube icon">
</a>
<a href="#">
<img src="/img/twitter-logo-button.png" alt="youtube icon">
</a>
<a href="#">
<img src="/img/facebook-logo-button.png" alt="youtube icon">
</a>
</div>
</footer>
</div>
</body>
</html>
</div>