【问题标题】:adding image attachement to php messages?将图像附件添加到 php 消息?
【发布时间】:2012-12-03 21:15:00
【问题描述】:

我的网站上有一个 php 消息系统。有了它,用户可以互相发送和接收消息,但最近我一直在尝试寻找一种包含图像附件的方法,以便用户可以发送带有消息的照片。

消息存储在 ptb_messages 中,消息部分(主题和正文)工作正常,但我在表中创建了一个名为“image”的列,它是一个 BLOB 类型和一个“名称”列来存储图像名称.但是我是 php 和 mysql 的新手,无论我尝试什么,我似乎都无法将图像存储在数据库中。

谁能帮助我,让我知道我哪里出错了?

<?php ob_start(); ?>

<?php 

// CONNECT TO THE DATABASE
    require('includes/_config/connection.php');
// LOAD FUNCTIONS
    require('includes/functions.php');
// GET IP ADDRESS
    $ip_address = $_SERVER['REMOTE_ADDR'];

?>


  <?php require_once("includes/sessionframe.php"); ?>


<?php
    confirm_logged_in();
    if (isset ($_GET['to'])) {
       $user_to_id = $_GET['to'];
    }
?> 
<?php 
//We check if the form has been sent
if(isset($_POST['subject'], $_POST['message_content']))
{
    $subject = $_POST['subject'];
    $content = $_POST['message_content'];
    $image = $POST ['image'];

        //We remove slashes depending on the configuration
        if(get_magic_quotes_gpc())
        {
                $subject = stripslashes($subject);
                $content = stripslashes($content);
        $image = stripslashes($image);      
        }

        //We check if all the fields are filled
        if($_POST['subject']!='' and $_POST['message_content']!='')
        {
$sql = "INSERT INTO ptb_messages (id, from_user_id, to_user_id, subject, content, image) VALUES (NULL, '".$_SESSION['user_id']."', '".$user_to_id."', '".$subject."', '".$content."', '".$image."');";
            mysql_query($sql, $connection);

            echo "<div class=\"infobox2\">The message has successfully been sent.</div>";
        }
}
if(!isset($_POST['subject'], $_POST['message_content']))

if (empty($_POST['subject'])){
    $errors[] = 'The subject cannot be empty.';
    if (empty($_POST['body'])){
       $errors[] = 'The body cannot be empty.';
    }
}

{
?>

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
  <div class="subject">
  <input name="subject" type="text" id="subject" placeholder="Subject">
  <input type="file" name="image" id="image">
  <textarea name="message_content" id="message_content" cols="50" placeholder="Message" rows="8" style="resize:none; height: 100px;"></textarea>
  <input type="image" src="assets/img/icons/loginarrow1.png" name="send_button" id="send_button" value="Send">
</form>

<?php } ?>

<?php ob_end_flush() ?>

【问题讨论】:

标签: php mysql image upload message


【解决方案1】:

我的建议是将图像的 URL 存储在数据库中,而不是图像文件本身。将图像存储在服务器文件系统中。原因在于备份和性能的概念,其中移动一个巨大的 blob 列重复多次并不是一件好事。另外,如果有人在没有 LIMIT 子句的情况下编写 SELECT *,您将获得传输所有图像的表扫描。

也就是说,如果您坚持将图像存储在数据库表中,您可能希望使用 base64_encode() 使图像文件对二进制传输安全。在将图像发送到浏览器之前,您会调用相应的解码函数。

http://php.net/manual/en/function.base64-encode.php

HTH,~雷

【讨论】:

    猜你喜欢
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 2013-01-01
    • 2020-07-27
    • 1970-01-01
    • 2013-02-07
    • 2010-10-06
    相关资源
    最近更新 更多