更新输入类型=“文件”并保留图像的路径

时间:2016-09-05 07:27:16

标签: php file-upload

这是我的代码

<?php
include "../../../config/config.php";
session_start();

if (isset($_GET['id'])) {
  $id = $_GET['id'];
} else {
  die("Not found");
}

if (isset($_POST['submit-edit'])) {

  $title = mysqli_real_escape_string($con, $_POST['title']);
  $description = mysqli_real_escape_string($con, $_POST['description']);
  $category = mysqli_real_escape_string($con, $_POST['category']);



  /* ----------------------- MAIN IMAGE  -------------------------- */
  $target_dir = "../../../img/find/thumbs-categorii/";
  $target_file2 = "" . basename($_FILES["img-edit"]["name"]);
  $target_file = $target_dir . basename($_FILES["img-edit"]["name"]);

  $uploadOk = 1;
  $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);

  // Check file size
  if ($_FILES["img-edit"]["size"] > 100000) {
    $_SESSION['image-size'] = 1;
    exit();
  }
//    
  //Allow certain file formats
  if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
      && $imageFileType != "gif" && $imageFileType != NULL) {
    $_SESSION['image-format'] = 1;

    exit();
  }

  // Check if $uploadOk is set to 0 by an error
  if ($uploadOk == 0) {

    // if everything is ok, try to upload file
  } else {
    if (move_uploaded_file($_FILES["img-edit"]["tmp_name"], $target_file)) {



    } else {
      echo "Sorry, there was an error uploading your file.";
//            exit();
    }


    $query = "UPDATE descopera_second" .
             "SET title='" . $title . "', text='" . $description . "', image='" .
             $target_file2 . "', fk_descopera_first='" . $category .
             "' WHERE id=" . $id;

    var_dump($query);
    exit();
    $result = mysqli_query($con, $query);
//                var_dump($query);
//                exit();
    if ($result) {

      $_SESSION['edit_slider'] = 1;
      header("Location: /dashboard/");
    } else {
//          

      header("Location: /dashboard/");
    }
  }
}
?>

如果带有图像的输入为空,我想将当前路径保留在数据库中。我不知道为什么,但我的代码目前停在echo "Sorry, there was an error uploading your file.";并在我的数据库中更改了图像的路径。如果输入为空,我想保留当前路径,因为我只想编辑。

2 个答案:

答案 0 :(得分:0)

if (move_uploaded_file($_FILES["img-edit"]["tmp_name"], $target_dir)) {

答案 1 :(得分:0)

您定义$uploadOk = 1,但该值永远不会更改。因此,以下代码始终执行条件的else部分。

if ($uploadOk == 0) {

} else {

    /* Everything in here will be executed */

}

现在查看那个条件块内的所有代码。

首先是另一个条件。

if (move_uploaded_file($_FILES["img-edit"]["tmp_name"], $target_file)) {

} else {
    echo "Sorry, there was an error uploading your file.";
    // exit();
}

您说代码停止的地方意味着move_uploaded_file失败。如果用户未在请求中提供文件上载,则会出现这种情况。因为$target_file将为空,并且空字符串可能不会成为文件系统上的有效路径,所以即使首先没有文件上传也是如此它是。

无论如何,无条件地继续发生在第一个条件块内发生的所有其他事情(即,即使没有上传文件,更新您的数据库)。

$query = "UPDATE descopera_second" .
         "SET title='" . $title . "', text='" . $description . "', image='" .
         $target_file2 . "', fk_descopera_first='" . $category .
         "' WHERE id=" . $id;

在发生这种情况时,请尝试使用var_dump检查$target_file2的值。它不会是你的预期。我们知道这一点,因为您将其初始化为上面的$target_file2 = "" . basename($_FILES["img-edit"]["name"]);,并且没有文件上传。所以$_FILES是空的。

var_dump($target_file2);

因此,更好的方法是在尝试更新数据库中的此值之前检查是否首先发生了有效的文件上载。您可能意味着使用顶部的$uploadOk变量执行此操作。

if (isset($_FILES["img-edit"]["tmp_name"])) {
    $query = "UPDATE descopera_second" .
             "SET title='" . $title . "', text='" . $description . "', image='" .
             $target_file2 . "', fk_descopera_first='" . $category .
             "' WHERE id=" . $id;
} else {
    $query = "UPDATE descopera_second" .
             "SET title='" . $title . "', text='" . $description . "'," .
             ", fk_descopera_first='" . $category .
             "' WHERE id=" . $id;
}