【问题标题】:Can't getting the image to upload on mysql longblob无法将图像上传到 mysql longblob
【发布时间】:2017-12-03 16:11:33
【问题描述】:

当我尝试上传图片时,它的字段为空。我不知道为什么。 请帮忙..

html表单:

<form action="edit_profile.php" method="POST" enctype="multipart/formdata">
    <input type="file" name="profile_image" required="">
    <button name="upload_image" class="btn btn-danger">Go</button>
</form>

php:

if(isset($_POST['upload_image'])){
        $image = $_FILES['profile_image']['tmp_name'];
        $profile_image = addslashes(file_get_contents($image));

        $sql = "update users set image='".$profile_image."' where username='$username'";
        $result = mysqli_query($con, $sql);

        if($result){
            echo "<script>window.open('profile.php','_SELF')</script>";
        }
        else{
            echo "<script>alert('Error!')</script>";
            echo "<script>window.open('profile.php','_SELF')</script>";
        }
    }

我得到空的图像字段..

【问题讨论】:

  • 您的表单的enctype 应该是multipart/form-data,而不是multipart/formdatastackoverflow.com/questions/35417567/… 的可能重复项
  • @Subham,你在哪里声明了 $username?还有一件事你必须在按钮中使用 type="submit"
  • @NarendraVerma $username 是我的会话用户名,我忘了编辑有问题的。如果我使用 而不是
  • 将图像直接保存在数据库中,哎呀。我曾经有一个 250GB 的数据库,其中包含图像。这是一个他们希望我重建的旧网站。这是不可能的工作。 (实际上这是一个邮购新娘网站......哈哈)
  • @kmoser,非常感谢您.. 输入错误.. 我键盘上的连字符很难。所以我轻轻按了..

标签: php html forms


【解决方案1】:

试试这个

您必须从 enctype="multipart/formdata" 更改为 enctype="multipart/form-data"

我在按钮上添加了type="submit"。我知道默认是submit

HTML 代码

<form action="process.php" method="post" enctype="multipart/form-data">
    <input type="file" name="profile_image"  required="">
    <button type="submit" name="upload_image" class="btn btn-danger">Go</button>
</form>

Process.php

在此处添加您的数据库文件

if (isset($_POST["upload_image"])) {
 if ($_FILES["profile_image"]["error"] > 0) {
            $error = $_FILES["file"]["error"];
        }
            else if (($_FILES["profile_image"]["type"] == "image/gif") ||
            ($_FILES["profile_image"]["type"] == "image/jpeg") ||
            ($_FILES["profile_image"]["type"] == "image/png") ||
            ($_FILES["profile_image"]["type"] == "image/pjpeg")) {

            $temp = explode(".", $_FILES["profile_image"]["name"]);
            $newfilename = round(microtime(true)) . '.' . end($temp);
            //echo $newfilename;
            $url = 'upload/'.$newfilename;// it will store the image in the upload folder
            $filename = compress_image($_FILES["profile_image"]["tmp_name"], $url, 80);//this will call the compress_image fruntion for optimize the image
        }else {
            $error = "Uploaded image should be jpg or gif or png";
        }

 $new_image_name=$newfilename;//here is the new random file name

 $sql = "update users set image='".$new_image_name."' where  username='$username'";
        $result = mysqli_query($conn, $sql);
        if($result){
            echo "<script>window.open('profile.php','_SELF')</script>";
        }
        else{
            echo "<script>alert('Error!')</script>";
            echo "<script>window.open('profile.php','_SELF')</script>";
        }
}

下面的函数会优化图像。

  $name = ''; $type = ''; $size = ''; $error = '';
   function compress_image($source_url, $destination_url, $quality) {

      $info = getimagesize($source_url);

          if ($info['mime'] == 'image/jpeg')
          $image = imagecreatefromjpeg($source_url);

          elseif ($info['mime'] == 'image/gif')
          $image = imagecreatefromgif($source_url);

          elseif ($info['mime'] == 'image/png')
          $image = imagecreatefrompng($source_url);

          imagejpeg($image, $destination_url, $quality);
          return $destination_url;
        }

【讨论】:

    猜你喜欢
    • 2019-01-16
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 2020-07-07
    • 2017-01-29
    • 2015-05-15
    相关资源
    最近更新 更多