PHP将图像上传到数据库

时间:2014-04-05 09:38:05

标签: php mysql

我在将图像添加到数据库时遇到问题,它插入所有填充图像获取错误并且未添加图像。 这是代码:

<?php 
include '../dbc.php'; 

$message='';
if(isset($_POST["submit"])){
  $name = $_POST["name"];
  $descr = $_POST["descr"];
  $price = $_POST["price"];
  $quantity = $_POST["quantity"];
  $item_number = $_POST["item_number"];
  $cat = $_POST["cat"];
  $file = $_FILES['image']['tmp_name']; 


 if((!$name) || (!$desc) || (!$price) || (!$quantity) || (!$item_number) || 
    (!$cat) || (!$image)){
    $message = "Fill all inputs!";
  }
  $name_query = mysql_query("SELECT name FROM products WHERE name='$name' 
                      LIMIT 1") or die("Can't check name!");
      $count_name = mysql_num_rows($name_query);

  if($count_name > 0){
        $message = "This product exist!";
  }else{
        $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));

        $query= mysql_query("INSERT INTO products (name, description, 
                price,quantity, item_number, category, image) VALUES 
                ('$name', '$descr', '$price', '$quantity', '$item_number',
                '$cat', '$image')") or die("Can't addi");
        $message="Product is added!"; 
    }
} 

?>

Theres错误:警告:file_get_contents():文件名不能为空

1 个答案:

答案 0 :(得分:1)

不建议将图像添加到数据库中,将图像上传到&#34;磁盘&#34;并将引用(&#34; /images/folder/image.jpg")保存到数据库中。

http://davidwalsh.name/basic-file-uploading-php