PHP映像上传在localhost上有效,但在服务器上不起作用

时间:2019-02-19 15:40:14

标签: php mysql forms image image-uploading

图像上传在本地主机上工作正常,但在实时服务器中无法正常工作.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";
}
?>

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

1 个答案:

答案 0 :(得分: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?我认为您必须在该文档中包括您的连接,否则它将不会将数据发送到您的数据库。