我尝试使用php构建一个上传图像系统。经过测试,系统回显出“图像上传”,但它没有显示在数据库中。这里是我的代码
upload.php的
<?php
//connect database
include('config.php');
//file properties
if(isset($_FILES['image'])){
$file = $_FILES['image']['tmp_name'];
}
if(!isset($file)){
echo "Please select an image";
}
else{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name =addslashes($_FILES['image']['name']);
$image_size =getimagesize($_FILES['image']['tmp_name']);
if ($image_size == FALSE) {
echo "This is not an image";
}
else{
$insert = "INSERT INTO image(name,picture) VALUES ('$image_name','$image')";
if(!$insert){
echo "Problem uploading";
}
else {
$lastid =mysqli_insert_id($con);
echo "Image Uploaded <p />Your Image :<p /><img src=get.php?id=$lastid>";
}
}
}
?>
get.php
<?php
include ('config.php');
$id = addslashes($_REQUEST['id']);
$image = mysqli_query($con ,"SELECT * FROM image WHERE id=$id");
$image = mysqli_fetch_assoc($image);
$image = $image['picture'];
header("Content-type :image/jpeg");
?>
然后点击提交按钮而不选择显示此2行警告的任何文件
警告:file_get_contents():在upload.php第14行中,文件名不能为空。
第14行是$ image = addslashes(file_get_contents($ _ FILES ['image'] ['tmp_name']));
警告:getimagesize():在upload.php第16行中,文件名不能为空。
第16行是$ image = addslashes(file_get_contents($ _ FILES ['image'] ['tmp_name']));
对此有何想法?
答案 0 :(得分:2)
您需要一个函数来发送您的查询,否则您只需填写一个字符串:this:
$insert = "INSERT INTO image(name,picture) VALUES ('$image_name','$image')";
应该遵循:
mysqli_query($con, $insert);
警告是由代码的多个问题引起的。首先,您要检查文件是否以错误的方式上传:此
if(isset($_FILES['image'])){
$file = $_FILES['image']['tmp_name'];
}
即使没有在表单中选择任何文件,也会始终设置$file
变量,因此导致永远不会执行此if语句:
if(!isset($file)){
echo "Please select an image";
}
并且始终执行else块中的内容,这会导致错误,因为您提到的函数(包含在此else块中)无法对任何文件进行操作。
因此,只需正确检查文件上传就可以解决问题:一种方法是首先删除它,这是无用的
if(isset($_FILES['image'])){
$file = $_FILES['image']['tmp_name'];
}
然后改变这个:
if(!isset($file)){
echo "Please select an image";
}
到此:
if(!isset($_FILES['image']['tmp_name'])){
echo "Please select an image";
}
答案 1 :(得分:1)
在第16行之后,您需要回显图像数据。
echo $ image;
@ken,这是我用来通过设置正确的标题来输出图像的函数:
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 . '"');
}
这是一个名为showimg.php.jpg
的php文件的一部分这需要使用以下.htaccess文件保存到单独的目录中,以使php能够处理.jpgs的请求:
AddType application/x-httpd-php .jpg
这样,网页就会引用图片<img src='/img/showimg.php.jpg?<add your parameters here>
问候 史蒂夫