【问题标题】:PHP: Upload images from folder to MySQL DatabasePHP:将图像从文件夹上传到 MySQL 数据库
【发布时间】:2014-02-25 05:10:03
【问题描述】:

我对此进行了很多搜索,但没有找到解决方案。

我在服务器的目录中有图像。我想使用 longblob 和 PHP 将这些图像(不仅仅是图像路径)上传到 MySQL 数据库。

我知道不建议将图像存储在数据库中,但这是我项目的要求,所以我想使用这种方法。

请建议我,我该怎么做? 谢谢大家。

【问题讨论】:

  • 现在我认为没有理由不将图像、pdf 或其他任何东西直接存储到数据库中。 5 年前可能是这样,但空间不再是问题,CPU 功率也不再是问题。

标签: php mysql upload mysqli


【解决方案1】:
<?php
$con = mysql_connect("localhost","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());

}
mysql_select_db("your databse name");

// Make sure the user actually
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];
$name=$_POST['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 image2 ";
$query .= "(image,name) VALUES ('$data','$name')";
$results = mysql_query($query, $con);

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

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

// Close our MySQL Link
mysql_close($con);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多