Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Fail to upload image

Tags:

php

test.php file

<?php

    if(isset($_POST['submit']))
    {
        if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
        {
            echo "Please select an image";
        }
        else
        {
            $image = addslashes($_FILES['image']['tmp_name']);
            $image = file_get_contents($image);
            $image = base64_encode($image);
            saveimage($image);
        }
    }

    function saveimage($image)
    {
        $con=mysql_connect("localhost","root","");

        mysql_select_db("food",$con);

        $query = "INSERT INTO info(image) VALUES ('$image')";
        $result = mysql_query($query,$con);

        if($result)
        {
            echo "<br> Image upload";
        }
        else
        {
            echo "<br> NOT";
        }
    }

function displayimage()
    {
        $con=mysql_connect("localhost","root","");

        mysql_select_db("food",$con);

        $query = "SELECT image from info";
        $result = mysql_query($query,$con);
        $row=mysql_fetch_array($result);
    echo '<img src="data:image/jpeg;base64,'.$row["image"].'" width="200" height="200"/>';
    }



?>

html file

<form method="post" action="test.php">

    <label>Image:</label>
    <input type="file" name="image"><br>

    <input class="btn btn-primary" type="submit" name="submit"  value="Confirm" style="height:50px; width:100px;">

    <br><br>

</form>

Now i want to display the picture. But i keep getting a blank picture instead. Is there any problem with my display code there? Thanks

/dummy//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa//dummy

like image 628
ron Avatar asked Aug 18 '26 18:08

ron


1 Answers

add enctype="multipart/form-data" in your form

<form method="post" action="test.php" enctype="multipart/form-data">
like image 107
Md. Sahadat Hossain Avatar answered Aug 20 '26 08:08

Md. Sahadat Hossain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!