1

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

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

<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>

我的数据库表是:

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

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";
  }

提交表单时出现错误:No image selected/uploaded

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

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

4

2 回答 2

1

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

<?

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>

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

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

于 2012-05-31T16:41:05.070 回答
0

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

<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';
?>
于 2013-01-11T11:17:08.623 回答