上传图片到mysql数据库 php[英] upload image to mysql database php

本文是小编为大家收集整理的关于上传图片到mysql数据库 php的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想将图像上传到PHP数据库,我的PHP代码

<?php
session_start();
include('../../includes/connect.php');

$title = $_POST['title'];
$subject = $_POST['subject'];
$file = $_FILES['image']['tmp_name'];
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_size = getimagesize($_FILES['image']['tmp_name']);
$visiable = 1;

$query = "insert into news (title, subject, image, visiable) values ('$title','$subject', '$image', '$visiable')"; 
$result = mysql_query($query);
$id = mysql_insert_id();

$data = array(
        'id' => $id
        );
$base = '../../show.php';
$url = $base. '?' . http_build_query($data);
header("Location: $url");
exit();?>

但是它没有上传图像字段仍然是0字节,它昨天很适合我,但是今天我不知道发生了什么,我希望有人可以帮助我,对不起,我的英语不好

推荐答案

这是我修改了与您的表结构一起工作的脚本.

function addImageToDB($imageArray, $title = '', $subject = '', $visible = 0) {

$allowedExts = array("gif","jpeg","jpg","JPG","png","PNG");
$extension = end(explode(".", $imageArray["name"]));

if (
    (($imageArray["type"] == "image/gif") // is image type acceptable?
        || ($imageArray["type"] == "image/jpeg")
        || ($imageArray["type"] == "image/jpg")
        || ($imageArray["type"] == "image/png")
    )
    && ($imageArray["size"] < 1048576) // set maximum image size
    && in_array($extension, $allowedExts) // is image file extension in $allowedExts?
) {

    if ($imageArray["error"] > 0) { // check uploaded image for errors
        echo $imageArray['error'];
    } else {

        $tempImage = $imageArray['tmp_name'];
        $fp = fopen($tempImage, 'r');
        $image = fread($fp, filesize($tempImage));
        $image = addslashes($image);
        fclose($fp);

        $queryAddImageToDB = "INSERT INTO image (
            title,
            subject,
            image,
            visible
        ) VALUES (
            '$title'
            '$subject',
            '$image',
            '$visible'
        )";

        mysql_query ($queryAddImageToDB) or die ('queryAddImageToDB failed');
        $imageID = mysql_insert_id();

        return $imageID;

    }
} else {

    echo 'IMAGE UPLOAD ERROR: The image ie either too large or the file format is unacceptable.';

    echo '<pre>';
        print_r($imageArray); // display image array for debugging
    echo '</pre>';

}

}

您可以这样调用该函数:

$imageArray = $_FILES['image'];
$title = $_POST['title'];
$subject = $_POST['subject'];
$visible = 1;
addImageToDB($imageArray, $title, $subject, $visible);

请注意,此脚本不完整,因为它需要适当的验证,逃避等等.

祝你好运,我希望这对您有利,我期待听到反馈.

本文地址:https://www.itbaoku.cn/post/597364.html

问题描述

i want to upload an image to php database, my php code

<?php
session_start();
include('../../includes/connect.php');

$title = $_POST['title'];
$subject = $_POST['subject'];
$file = $_FILES['image']['tmp_name'];
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_size = getimagesize($_FILES['image']['tmp_name']);
$visiable = 1;

$query = "insert into news (title, subject, image, visiable) values ('$title','$subject', '$image', '$visiable')"; 
$result = mysql_query($query);
$id = mysql_insert_id();

$data = array(
        'id' => $id
        );
$base = '../../show.php';
$url = $base. '?' . http_build_query($data);
header("Location: $url");
exit();?>

but it upload nothing the image field is still 0 byte, it works me yesterday but today i dont know what happened, I hop that someone can help me, sorry for my bad english

推荐答案

Here's a script I modified to work with your table structure.

function addImageToDB($imageArray, $title = '', $subject = '', $visible = 0) {

$allowedExts = array("gif","jpeg","jpg","JPG","png","PNG");
$extension = end(explode(".", $imageArray["name"]));

if (
    (($imageArray["type"] == "image/gif") // is image type acceptable?
        || ($imageArray["type"] == "image/jpeg")
        || ($imageArray["type"] == "image/jpg")
        || ($imageArray["type"] == "image/png")
    )
    && ($imageArray["size"] < 1048576) // set maximum image size
    && in_array($extension, $allowedExts) // is image file extension in $allowedExts?
) {

    if ($imageArray["error"] > 0) { // check uploaded image for errors
        echo $imageArray['error'];
    } else {

        $tempImage = $imageArray['tmp_name'];
        $fp = fopen($tempImage, 'r');
        $image = fread($fp, filesize($tempImage));
        $image = addslashes($image);
        fclose($fp);

        $queryAddImageToDB = "INSERT INTO image (
            title,
            subject,
            image,
            visible
        ) VALUES (
            '$title'
            '$subject',
            '$image',
            '$visible'
        )";

        mysql_query ($queryAddImageToDB) or die ('queryAddImageToDB failed');
        $imageID = mysql_insert_id();

        return $imageID;

    }
} else {

    echo 'IMAGE UPLOAD ERROR: The image ie either too large or the file format is unacceptable.';

    echo '<pre>';
        print_r($imageArray); // display image array for debugging
    echo '</pre>';

}

}

You can call the function like this:

$imageArray = $_FILES['image'];
$title = $_POST['title'];
$subject = $_POST['subject'];
$visible = 1;
addImageToDB($imageArray, $title, $subject, $visible);

Please note that this script IS NOT COMPLETE as it needs proper validation, escaping, etc.

Good luck I hope this works out for you and I look forward to hearing feedback otherwise.