php 通过php上传MySql数据库中的图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10837556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:10:54  来源:igfitidea点击:

To upload a image in MySql database via php

phpmysqlhtml

提问by Neeraj

I am currently working on a website that needs to upload the images of different products by its users. I am implementing it by using MySql database via php.

我目前正在开发一个需要用户上传不同产品图片的网站。我正在通过 php 使用 MySql 数据库来实现它。

My code for a basic form for taking input from users is:

我用于获取用户输入的基本表单的代码是:

<form enctype="multipart/form-data" action="testimage1.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>

My database table is:

我的数据库表是:

 mysql> CREATE TABLE tbl_images (
 > id tinyint(3) unsigned NOT NULL auto_increment,
 > image blob NOT NULL,
 > PRIMARY KEY (id)
 > );

testimage1.php has the following code:-

testimage1.php 有以下代码:-

 $username = "root";
 $password = "";
 $host = "localhost";
 $database = "thinstrokes";


 $link = mysql_connect($host, $username, $password);
 if (!$link) {
 die('Could not connect: ' . mysql_error());
 }

// Select your database
mysql_select_db ($database);

    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

  // Temporary file name stored on the server
  $tmpName  = $_FILES['image']['tmp_name'];  

  // Read the file 
  $fp      = fopen($tmpName, 'r');
  $data = fread($fp, filesize($tmpName));
  $data = addslashes($data);
  fclose($fp);


  // Create the query and insert
  // into our database.
  $query = "INSERT INTO tbl_images ";
  $query .= "(image) VALUES ('$data')";
  $results = mysql_query($query, $link) or die(mysql_error());

  // Print results
  print "Thank you, your file has been uploaded.";

  }
  else {
  print "No image selected/uploaded";
  }

On submitting the form I am getting an error: No image selected/uploaded

在提交表单时,我收到一个错误: No image selected/uploaded

I am not getting the error... and I've already asked for this before as:

我没有收到错误......我之前已经要求过这个:

But until now I am not successful in storing the image in the database.

但是直到现在我还没有成功地将图像存储在数据库中。

采纳答案by Ahoura Ghotbi

Your script is working just fine, This is what I test:

你的脚本工作得很好,这是我测试的:

<?

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

  // Temporary file name stored on the server
  $tmpName  = $_FILES['image']['tmp_name'];  

  // Read the file 
  $fp      = fopen($tmpName, 'r');
  $data = fread($fp, filesize($tmpName));
  $data = addslashes($data);
  fclose($fp);


  // Create the query and insert

  // Print results
  print "Thank you, your file has been uploaded.";

  }
  else {
  print "No image selected/uploaded";
  }

?>

<form enctype="multipart/form-data" action="" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>

And it works just fine, if you want to see it in action I can send you the link.

它工作得很好,如果你想看到它的实际效果,我可以给你发送链接。

It must be something else that is ruining your code (*note that I removed the DB queries to avoid getting mysql errors but the script was working even with them there.

一定是其他东西破坏了您的代码(*请注意,我删除了数据库查询以避免出现 mysql 错误,但即使在那里脚本也能与它们一起工作。

回答by Sanjiv Kumar

//Here is the solution for your problem

//这里是你的问题的解决方案

<form enctype="multipart/form-data" action="" method="post" name="changer">
    <input name="MAX_FILE_SIZE" value="102400" type="hidden">
    <input name="image" accept="image/jpeg" type="file">
    <input value="Submit" type="submit">
</form>
<?php

    // connection to database
    include 'includes/connection.php';
?>

<?php

    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 
        // Temporary file name stored on the server
        $tmpName  = $_FILES['image']['tmp_name'];  
        // Read the file 
        $fp      = fopen($tmpName, 'r');
        $data = fread($fp, filesize($tmpName));
        $data = addslashes($data);
        fclose($fp);
        $result = mysql_query("INSERT INTO image (image)VALUES ( '$data')", $connection);
        if(!$result)
        {
            die("Database query failed: ". mysql_error());
        }
        // Print results

        print "Thank you, your file has been uploaded.";
    }
    else 
    {
        print "No image selected/uploaded";
    }
?>

<?php
    //close connection
    include 'includes/close.php';
?>