php - 不显示来自数据库和文件的图像

标签 php mysql database

我已经建立了一个网站,允许用户上传图片,然后这些图片会显示在主屏幕上。但是我刚才碰壁了,所以我不再让用户上传图像,然后将图像保存到文件夹和数据库中,但是我该怎么做才能将图像显示在主屏幕上。

 <?php

 // Connect to database

 $errmsg = "";
 if (! @mysql_connect("localhost","alfred1000351","*******")) {
 $errmsg = "Cannot connect to database";
     }
 @mysql_select_db("drp_2cgih5o233");



 $q = <<<CREATE
 create table pix (
 pid int primary key not null auto_increment,
 title text,
 imgdata longblob)
 CREATE;
 @mysql_query($q);

 // Insert any new image into database

  if ($_REQUEST[completed] == 1) {
  // Need to add - check for large upload. Otherwise the code
  // will just duplicate old file ;-)
 // ALSO - note that latest.img must be public write and in a
 // live appliaction should be in another (safe!) directory.
 move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
 $instr = fopen("latest.img","rb");
 $image = addslashes(fread($instr,filesize("latest.img")));
 if (strlen($instr) < 149000) {
 mysql_query ("insert into pix (title, imgdata) values (\"".
 $_REQUEST[whatsit].
 "\", \"".
 $image.
 "\")");
 } else {
 $errmsg = "Too large!";
 }
 }

 // Find out about latest image

  $gotten = @mysql_query("select * from pix order by pid desc limit 1");
  if ($row = @mysql_fetch_assoc($gotten)) {
  $title = htmlspecialchars($row[title]);
  $bytes = $row[imgdata];
  } else {
  $errmsg = "There is no image in the database yet";
  $title = "no database image available";
  // Put up a picture of our training centre
  $instr = fopen("../wellimg/ctco.jpg","rb");
   $bytes = fread($instr,filesize("../wellimg/ctco.jpg"));
  }

 // If this is the image request, send out the image

if ($_REQUEST[gim] == 1) {
 header("Content-type: image/jpeg");
print $bytes;
exit ();
}
?>

<html><head>
<title>Upload an image to a database</title>
<body bgcolor=white><h2>Here's the latest picture</h2>
<font color=red><?= $errmsg ?></font>
<center><img src=?gim=1 width=144><br>
<b><?= $title ?></center>
<hr>
<h2>Please upload a new picture and title</h2>
<form enctype=multipart/form-data method=post>
<input type=hidden name=MAX_FILE_SIZE value=150000>
<input type=hidden name=completed value=1>
Please choose an image to upload: <input type=file name=imagefile><br>
Please enter the title of that picture: <input name=whatsit><br>
then: <input type=submit></form><br>
<hr>

</body>
</html>

最佳答案

这是我的强项:

  1. 不要抑制从 DB 获得的警告。他们是真实的,需要的。从 DB 调用的前面删除所有 @ 字符。 如果您收到任何通知、警告、错误,请不要压制它们,更正它们。

  2. 如果您要编写新代码,请考虑使用 PDO 作为 DB API,而不是旧的、已弃用的 PHP MySQL API。它和 MySQL API 一样易于使用。

  3. 您只想尝试只创建一次表,将它从执行多次的代码中移除。

  4. 您应该检查 $_REQUEST 参数是否存在,如果不存在则不要比较它们。您还需要将参数名称放在引号中,否则 PHP 会认为您使用的是常量,而实际上您不是。所以行if ($_REQUEST[completed] == 1) {必须固定为 if(isset($_REQUEST['completed']) && $_REQUEST['completed'] == 1) { .同样适用于 whatisitgim -参数。

  5. 代码if (strlen($instr) < 149000) {没有按预期工作,您无法获得资源的长度。您可能正在寻找此功能:if (strlen($_FILES['imagefile']['size']) < 149000) { .

  6. $row 相同(与步骤 4 相同)如果您需要将文字放在引号中,那么将这些行修复为:$title = htmlspecialchars($row['title']); $bytes = $row['imgdata'];

  7. 除此之外它应该可以工作。但是,它包含数据库安全漏洞,可能会危及您的网站,所以我建议您不要将它放到任何真实网站上。只是为了您自己/ friend 的乐趣。

关于php - 不显示来自数据库和文件的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10892286/

相关文章:

php 维护状态

javascript - 将逗号添加到 JSON 对象列表

mysql - 创建表 MYSQL 时出错 : SYNTAX

.net - 如何增加 MS Access 2007 数据库的大小?

php - woocommerce 类别深度作为 if 语句的条件

php - 是否有任何 html 模板引擎可以与 ruby​​ 和 php 一起使用?

mysql - 左连接中使用子查询时如何优化查询

php - 网页有重定向循环

mysql - Storm 直接从 MySQL 喷出?

sql - 在 SQL 中构造 FROM