douqian8238 2012-12-21 19:08
浏览 32
已采纳

发送带有邮件主题和正文的图像(可选)?

I've created a simple php messaging system for users to send and receive messages to each other. What i want to try and do is add in an optional image input field so the user can choose an image and send this as part of their message?

The photo would need to store in a directory folder on my server and the name of the image will need to be stored in mysql table along with the rest of the message fields i.e. content, subject, image_name.

Can someone show me how i could adapt my code to upload an image with the form please?

<?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() ?>
  • 写回答

1条回答 默认 最新

  • dousui3124 2012-12-21 19:25
    关注

    I just did some digging on Google and found this code. I think this should do what you want, but of course you will have to adapt it for your instance.

    <?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";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测