doushao1087 2015-12-10 07:09
浏览 31
已采纳

为什么使用PHP上传的图像未在longblob中正确存储

I am having issues storing a .JPG image in my MySQL database. I am using the PHP code below to store it into the database (in long blob form). My code appears to be completely error free and every thing seems to act as it should aside from the fact that the image is only stored in what appears to be 36 B size on PHPMyAdmin.

I would expect the image to be much larger (22 MB supposedly).

<?php
require '../FirstWebsite/CommonFunctions.php';
DB_Connect();
$dblink = DB_Connect();
?>
<html>
<body>
    <form method="post" enctype="multipart/form-data">
        <br/>
        <input type="file" name="selectImage">
        <br/>
        <input type="submit" name="submitImage" value="submit">
    </form>
    <?php
    if(isset($_POST['submitImage'])){
        if(getimagesize($_FILES['selectImage']['tmp_name'])==FALSE){
            echo 'please select an actual image';
        }
        else{
            $getImage = mysqli_real_escape_string($dblink,                        $_FILES['selectImage']['tmp_name']);
            $name = mysqli_real_escape_string($dblink, $_FILES['selectImage']['name']);
            $image = base64_encode($getImage);
            saveImage($name, $image, $dblink);
        }
    }
    function saveImage($name, $image, $dblink){
        $query = "INSERT INTO trialtable2 (caption, actualpicture) VALUES       ('$name','$image')";
        $queryCheck = mysqli_query($dblink, $query);
        if($queryCheck == TRUE){
            echo '<br/> uploaded';
        }
        else{
            echo '<br/> not uploaded';
        }
    }
    ?>
</body>
</html>

what I have done

  • attempted uploading .jpeg .JPG .jpg (same but still...) .tif .png

  • file_upload = on (or whatever) is already on in php.ini

  • it is a long blob type in the data base table

Supposedly storing images in a database is no the way to go however this is what I am working with for the time being.

versions

  • wamp: 2.5
  • apache: 2.4.9
  • mysql: 5.6.17
  • php: 5.5.12

I believe that this question is not a replica as I haven't been able to find an answer elsewhere. If I am doing something incorrectly feel free to let me know as I am still new to the community.

enter image description here

  • 写回答

1条回答 默认 最新

  • duanshan2988 2015-12-10 07:22
    关注

    tmp_name is the temporary path to the file, this is why you're only seeing a few bytes.

    • Remove any escaping (including addslashes).
    • use file_get_contents($_FILES['selectImage']['tmp_name']) to get the actual contents of the image for inserting into your database.

    It generally isn't a great idea to store the blobs in the database, and better to store the path (like you're currently doing). If you choose to go the path route, you need to use something like move_uploaded_file to move the file to a persistent location.

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

报告相同问题?

悬赏问题

  • ¥15 三分类机器学习模型可视化分析
  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
  • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
  • ¥30 python,LLM 文本提炼
  • ¥15 关于将inet引入的相关问题
  • ¥15 关于一个倒计时的操作和显示设计
  • ¥15 提问STK的问题,哪位航天领域的同学会啊
  • ¥15 苹果系统的mac m1芯片的笔记本使用ce修改器使用不了
  • ¥15 单相逆变的电压电流双闭环中进行低通滤波PID算法改进
  • ¥15 关于#java#的问题,请各位专家解答!