【发布时间】:2019-07-13 04:15:01
【问题描述】:
图像上传在 localhost 中工作正常,但在实时服务器中无法正常工作。HTML 表单在提交表单后显示成功消息,但没有数据存储在数据库中。我尝试打印错误但没有错误。我的代码:
<?php
include './login_validity.php';
$res = '';
$mes = '';
$img = NULL;
if (isset($_POST['submit'])) {
$name = $_POST['title'];
$des = $_POST['description'];
$shrt_detail = $_POST['shrt_description'];
$date = $_POST['date'];
$show_status = $_POST['is_show'];
$detail = $des;
//------------------image upload---------------------
if (!empty($_FILES["fileToUpload"]["name"])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if ($check !== false) {
$uploadOk = 1;
} else {
$_SESSION['message'] = "File is not an image.";
$uploadOk = 0;
}
// Allow certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "tif")
{
$_SESSION['message'] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$_SESSION['message'] = "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$_SESSION['message'] = "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.";
} else {
$_SESSION['message'] = "Sorry, there was an error uploading your file.";
}
}
$img = $target_file;
}
$query = "INSERT INTO slider (title,image,shrt_description,description,date,is_show) VALUES ('$name','$img','$shrt_detail','$detail','$date','$show_status')";
$res = $conn->query($query);
$mes = "Successfully Added";
}
?>
提交表单后,我检查了服务器中的上传文件夹。我在该文件夹中找到了上传的文件。但是当我检查 phpMyadmin 时,数据库中没有关于该上传文件的数据。但是当我在 localhost 中运行此页面时,一切正常。
【问题讨论】:
标签: php mysql forms image image-uploading