首页 > 解决方案 > PHP图像上传在本地主机上工作,但在服务器上不工作

问题描述

图片上传在本地主机中工作正常,但在实时服务器中无法正常工作。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 中运行此页面时,一切正常。

标签: phpmysqlformsimageimage-uploading

解决方案


我认为你的问题在这里:

$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;

您必须将其更改为:

$res = '';
$mes = '';
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;
    $img = $_FILES['fileToUpload']['name'];

如果你这样做$img = NULL;你没有收到它。您必须这样做$img = $_FILES['fileToUpload']['name'];才能从表单中获取提交的数据

PS-您在哪里声明变量$conn?我认为您必须在该文档中包含您的连接,否则它不会将数据发送到您的数据库。


推荐阅读