dqq9695 2013-07-30 01:20
浏览 65
已采纳

上传图片到mysql数据库php

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

  • 写回答

1条回答 默认 最新

  • doulai8128 2013-07-30 02:15
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看