Cant upload and store the image to the database by using php

夙愿已清 提交于 2019-12-07 04:59:24

You need a function to send your query, otherwise you just filled up a string: this:

$insert = "INSERT INTO image(name,picture) VALUES ('$image_name','$image')";

should be followed by this:

mysqli_query($con, $insert);

The warnings are caused by multiple issues with your code. First you are checking whether the file has been uploaded in the wrong way: this

if(isset($_FILES['image'])){
    $file = $_FILES['image']['tmp_name'];
}

Will always set a $file variable, even though no file has been selected into the form, leading therefore to never execute this if statement:

if(!isset($file)){
    echo "Please select an image";
}

and to always execute what's in the else block instead, which causes the errors, because the functions you mentioned, which are contained in this else block, are not able to operate on any file.

Therefore simply checking the file upload correctly will solve the issue: one way to do this would be to first remove this, which is unuseful

if(isset($_FILES['image'])){
    $file = $_FILES['image']['tmp_name'];
}

and then change this:

if(!isset($file)){
    echo "Please select an image";
}

to this:

if(!isset($_FILES['image']['tmp_name'])){
    echo "Please select an image";
}

After line 16 you need to echo the image data.

echo $image;

@ken, here is a function that I use to output the image by setting the correct headers:

    function output_headers($filetype, $imgsize, $filename, $attachment=false)
{
header("Content-Type: {$filetype}");
header("Pragma: public");
header("Cache-Control: public");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 * 24 * 4 -1;
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
header($ExpStr);
header("Cache-Control: max-age=$offset");
//header("Content-Type: application/octet-stream");
header('Accept-Ranges: bytes');
header("Content-Length: $imgsize");
#insert the filename we want into the image so it can be saved with something
# more meaningful that showimg.jpg. :-)
# 04/03/06
#attachment causes save as to appear, inline serves imbedded image.
if ($attachment)
    header('Content-Disposition: attachment; filename="' . $filename . '"');
else
    header('Content-Disposition: inline; filename="' . $filename . '"');
}

This is part of a php file called showimg.php.jpg

This needs to be saved into a separate directory with the following .htaccess file to get php to handle requests for .jpgs:

AddType application/x-httpd-php .jpg

In this way, the webpage refers to the image <img src='/img/showimg.php.jpg?<add your parameters here>

regards Steve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!