gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:45:06 25 4
gpt4 key购买 nike

我目前在一个网站上工作,该网站需要用户上传不同产品的图片。我通过 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

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

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

最佳答案

你的脚本工作正常,这是我测试的:

<?

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 错误,但脚本甚至在那里工作。

关于php - 通过php上传MySql数据库中的图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10837556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com