社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

php图片上传不工作,没有数据插入mysql

Hoàng Quân • 7 年前 • 2004 次点击  

我只是创建一个简单的图片上传到mysql

<form method="post" action="quanly.php" class="form-group justify-content-center" enctype="multipart/form-data">
               <div class="custom-file mb-3">
                 <input type="file" class="custom-file-input" id="image" name="image">
                 <label class="custom-file-label" for="customFile">Choose file</label>
               </div>
               <div class="col text-center">
                  <button type="submit" name="submit" id="add_btn" class="btn btn-primary mb-2"> <i class="fas fa-plus"></i>Submit</button>
               </div>
            </form>

我的PHP代码:

$image = addslashes(file_get_contents($_FILES['image']['name']));
   $query = "INSERT INTO tasks (image) VALUES ('$image')";
   mysqli_query($db, $query); //db is the mysql connection

这个 image 在mysql中是longblob。 问题:当我尝试提交图像时,数据库中没有数据

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39220
文章 [ 2 ]  |  最新文章 7 年前
Anil Loutombam
Reply   •   1 楼
Anil Loutombam    7 年前
<?php
$target_dir = "quanly/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

 $target_dir = "quanly/" - specifies the directory where the file is going to be placed.
 $target_file specifies the path of the file to be uploaded
 $uploadOk=1 is not used yet (will be used later)
 $imageFileType holds the file extension of the file (in lower case)

接下来,检查图像文件是实际图像还是假图像

试试这个……

Dilip Hirapara
Reply   •   2 楼
Dilip Hirapara    7 年前

去除 文件获取目录 从图像标签。

$image = addslashes($_FILES['image']['name']);
   $query = "INSERT INTO tasks (image) VALUES ('$image')";
   mysqli_query($db, $query); //db is the mysql connection