为什么我的照片上传不了,检索的时候照片是空白的?


Why can't my picture get uploaded and when it's retrieve the picture is blank

当我尝试上传它时,我得到的错误是这个错误:

警告:file_get_contents(MTUtODAwLmpwZw==):打开流失败:中没有这样的文件或目录C:'xampp'htdocs'miniproject'imageupload'image.php on line 24

我也已经在我的数据库中插入了一张图片,当它检索它只是显示一个木板图片。

这是我的代码

<?PHP
ini_set('mysql.connect_timeout', 300);
ini_set('default_socket_timeout', 300);
?>
<html>
    <body>
        <form method="post" enctype="multipart/form-data">
           <br/>
           <input type="file" name="image"/>
            <br/><br/>
           <input type="submit" name="sumit" value="Upload"/>
        </form>
    <?php
        if(isset($_POST['sumit']))
        {
            if (getimagesize($_FILES['image']['tmp_name'])==FALSE)
            {
                echo "Please select an image.";
            }
            else
            {
                $image= addslashes($_FILES['image']['name']);
                $name= addslashes($_FILES['image']['name']);
                $image= file_get_contents($image);
                $image= base64_encode($image);
                saveimage($name, $image);
            }
        }
        displayimage();
        function saveimage($name,$image)
        {
            $con=  mysql_connect("localhost:3307", "root", "");
            mysql_select_db("kstark",$con);
            $qry="insert into images (name, image) values ('$name','$image')";
            $result=  mysql_query($qry,$con);
            if ($result)
            {
                echo "<br/>Image uploaded.";
            }
            else
            {
                echo "<br/>Image not uploaded.";
            }
        }
        function displayimage()
        {
            $con=  mysql_connect("localhost:3307", "root", "");
            mysql_select_db("kstark",$con);
            $qry="select * from images";
            $result=  mysql_query($qry,$con);     
            while($row = mysql_fetch_array($result))
            {
                echo '<img height="300" width="300" src="data:image;base64,'.$row[2].'">';
            }
            mysql_close($con);
        }
    ?>
</body>

当您尝试检索内容时,您需要使用tmp_name而不是文件名。

if(isset($_POST['sumit']))
{
    if (getimagesize($_FILES['image']['tmp_name'])==FALSE) {
        echo "Please select an image.";
    } else {
        /* use the tmp_name rather than name here! */
        $image=file_get_contents( $_FILES['image']['tmp_name'] );
        $image=base64_encode($image);
        $name=addslashes($_FILES['image']['name']);
        saveimage($name,$image);
    }
}