【问题标题】:PHP image upload is working on localhost but not working on serverPHP图像上传在本地主机上工作,但在服务器上不工作
【发布时间】: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


    【解决方案1】:

    我认为你的问题在这里:

    $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?我认为您必须在该文档中包含您的连接,否则它不会将数据发送到您的数据库。

    【讨论】:

    • 你有sql注入漏洞。使用准备好的语句。不要接受原始的 $_POST 数据。此外,您正在查询中插入文字。 “$name”应该使用“.$name”转义。
    猜你喜欢
    • 2013-04-14
    • 1970-01-01
    • 2016-08-04
    • 2014-01-15
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多