如何在php mysql中上传图片

时间:2016-02-05 17:55:32

标签: php mysql

嗨,我试图将图像上传到数据库,但它正在数据库中插入tmp_name。任何人都可以帮我这个。

这是我的代码

<?php
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$title=$_POST['blog_title'];
$description=$_POST['blog_description'];
$name=$_FILES["image"]["tmp_name"];
$type=$_FILES["image"]["type"];
$size=$_FILES["image"]["size"];
$temp=$_FILES["image"]["tmp_name"];
$error=$_FILES["image"]["error"];
if($error>0)
die("error while uploading");
else
{
if($type == "image/png" || $type == "image/jpeg" ||$type == "image/jpg" || $type == "image/svg" || $size >2000000)
{       
move_uploaded_file($temp,"upload/".$name);
$sql=mysql_query("INSERT INTO blogs(image,blog_title,blog_description)values('$name','$title','$description')");
echo "upload complete";
}
else
{
die("Format not allowed or file size too big!");
}
}

我试图插入一个错误的图像

警告:move_uploaded_file(upload / C:\ xampp \ tmp \ php1908.tmp):无法打开流:第21行的C:\ xampp \ htdocs \ accounting \ admin \ blogs.php中的参数无效

警告:move_uploaded_file():无法移动&#39; C:\ xampp \ tmp \ php1908.tmp&#39; to&#39; upload / C:\ xampp \ tmp \ php1908.tmp&#39;

在数据库中,它插入为C:xampp mpphp1908.tmp

1 个答案:

答案 0 :(得分:0)

<?php
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$title=$_POST['blog_title'];
$description=$_POST['blog_description'];
$name=$_FILES["image"]["name"];
$type=$_FILES["image"]["type"];
$size=$_FILES["image"]["size"];
$temp=$_FILES["image"]["tmp_name"];
$error=$_FILES["image"]["error"];
if($error>0)
die("error while uploading");
else
{
if($type == "image/png" || $type == "image/jpeg" ||$type == "image/jpg" || $type == "image/svg" || $size >2000000)
{       
move_uploaded_file($temp,"upload/".$name);
$sql=mysql_query("INSERT INTO  blogs(image,blog_title,blog_description)values('$name','$title','$description')"    );
echo "upload complete";
}
else
{
die("Format not allowed or file size too big!");
}
}