图像不会上传到MYSQL数据库,但会显示在我的上传文件夹中

时间:2017-11-27 19:46:33

标签: php mysql database image upload

我用这段代码整夜都在伤脑筋。问题是,当我尝试将博客图像上传到我的数据库时,它不会出现在那里。然而它出现在我的上传文件夹中。有时在搞乱代码的时候我会把它上传但是图像本身看起来是空的。所有其他信息,如名称/大小和文件类型在我的数据库中显示正常。我有什么想法可能是错的吗?

<?php


require('config.php');
session_start();
if(isset($_POST['save']))
{
  $target_dir = "upload/img/";
  $filename = explode('.',$_FILES['image']['name']);
  $ext = $filename[1];
  $imgname = time().'.'.$ext;
  $target_file = $target_dir . $imgname ;
    $imgData =addslashes(file_get_contents($_FILES['image']['tmp_name'], $target_file));

  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  // Check if image file is a actual image or fake image
  $check = getimagesize($_FILES["image"]["tmp_name"]);
  if($check !== false) {
    $text="File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    $text="File is not an image.";
    $uploadOk = 0;
  }
  // Check if file already exists
  if(file_exists($target_file)) {
    $text="Sorry, file already exists.";
    $uploadOk = 0;
  }
  // Check file size
  if($_FILES["image"]["size"] > 2000000) {
    $text="Sorry, your file is too large.";
    $uploadOk = 0;
  }
  // Allow certain file formats
  if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  && $imageFileType != "gif" && $imageFileType != "bmp" ) {
    echo "Sorry, only JPG, JPEG, PNG, GIF & BMP files are allowed.";
    $uploadOk = 0;
  }
  // Check if $uploadOk is set to 0 by an error
  if($uploadOk == 0) {
    $_SESSION["error"]=$text;
    header("Location:index.php?id=$id"); /* Redirect browser */
    exit();

  // if everything is ok, try to upload file
  }else{
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
      $path=$imgname;
      $finfo = new finfo();
        $array = explode('.', $_FILES['image']['name']);
        $extension = mime_content_type($target_file);
        $filesize=$_FILES["image"]["size"];
        $filedata=$imgData;


      $conn->query("INSERT INTO images (image, name,filetype,size) VALUES (LOAD_FILE('$filedata','$path','$extension','$filesize')");
      $_SESSION["Success"]='Upload success';
      header("Location:index.php"); /* Redirect browser */
      exit();
    } else {
      $_SESSION["err"]=$text;
      header("Location:index.php"); /* Redirect browser */
      exit();
    }
  }
}



?>

1 个答案:

答案 0 :(得分:0)

我重新开始。此代码将图像/名称/大小和扩展名上传到MYSQL数据库。

<?php

require_once 'config.php';
$msg = "";

if (isset($_POST['save'])) {
        $target = "upload/img/".basename($_FILES['image']['name']);



        $image = $_FILES['image']['name'];
        $name = pathinfo($_FILES['image']['name'], PATHINFO_FILENAME);
        $allowed =  array('gif','png' ,'jpg');
        $filename = $_FILES['image']['name'];
        $extension = pathinfo($filename, PATHINFO_EXTENSION);
        $file_tmp = $_FILES['image']['tmp_name'];
        $filesize  = filesize($file_tmp);


        $conn->query("INSERT INTO images (image, name, filetype, size) VALUES ('$image','$name','$extension','$filesize')");

        if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
            $msg = "Image uploaded successfully";
             header("Location:index.php"); /* Redirect browser */
                exit();
        }else{
            $msg = "Failed to upload image";
        }
    }





?>