php - 发送带有消息主题和正文的图像(可选)?

标签 php mysql message

我创建了一个简单的 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() ?>

最佳答案

我刚刚在 Google 上进行了一些挖掘并找到了这段代码。我认为这应该可以满足您的需求,但您当然必须针对您的实例进行调整。

<?php
  $allowedExts = array("jpg", "jpeg", "gif", "png");
  $extension = end(explode(".", $_FILES["file"]["name"]));
  if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/png")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
      && ($_FILES["file"]["size"] < 20000)
      && in_array($extension, $allowedExts)){
           if ($_FILES["file"]["error"] > 0){
             echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
           }  else{
                echo "Upload: " . $_FILES["file"]["name"] . "<br>";
                echo "Type: " . $_FILES["file"]["type"] . "<br>";
                echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
                echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
           if (file_exists("upload/" . $_FILES["file"]["name"])){
             echo $_FILES["file"]["name"] . " already exists. ";
           }   else{
                 move_uploaded_file($_FILES["file"]["tmp_name"],
                 "upload/" . $_FILES["file"]["name"]);
                 echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
               }
           }
   }  else  {
        echo "Invalid file";
}
?>

关于php - 发送带有消息主题和正文的图像(可选)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13995807/

相关文章:

java - 无法执行,没有命名参数匹配

php - 使用 PHP 发送一封电子邮件,其中包含从数据库中获取的信息

php - 我如何在 php 中获取 mysql 警告消息(不是错误)

php - PHP 的 Yaml 扩展

PHP:如何为集合制作包装器,每次我调用它时都会返回集合的下一个元素

PHP从文本文件登录

php - 如何禁止直接访问浏览器网址中的网站目录,但在网站内部请求时允许?

MySQL:LOAD DATA LOCAL INFILE 添加额外字符 '\r'

java - Java 客户端组织消息数据库的不同方式

spring - Spring登录时显示错误消息