PHP映像上载正在localhost上工作但不在服务器上工作

问题描述 投票:0回答:1

图像上传在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";
}
?>

提交表单后,我检查了server中的uploads文件夹。我在该文件夹中找到了上传的文件。但是当我检查phpMyadmin数据库中没有关于该上传文件的数据时。但是当我在localhost中运行此页面时,一切正常。

php mysql forms image image-uploading
1个回答
0
投票

我认为你的问题在这里:

$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'];才能从表单中获取提交的数据

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

© www.soinside.com 2019 - 2024. All rights reserved.