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.

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

报告相同问题?