douhan8430 2015-06-23 09:41
浏览 38
已采纳

PHP - 成功上传文件后如何发送电子邮件? [重复]

This question already has an answer here:

I need a PHP to send an email if a file was uploaded successfully. My code is working fine like this:

<?php

  $target_dir = "gallery-sys/";
  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

  if (file_exists($target_file)) {
    echo "File already exists.";
    $uploadOk = 0;
  }

  if ($uploadOk == 0) {
    echo "Error - file was not uploaded.";
  } else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
       echo "File". basename( $_FILES["fileToUpload"]["name"]). " was uploaded successfully.";
    }
  }

?>

but I need email functionality. It doesn't even work anymore if I add those 6 lines:

<?php

  $target_dir = "gallery-sys/";
  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

  if (file_exists($target_file)) {
    echo "File already exists.";
    $uploadOk = 0;
  }

  if ($uploadOk == 0) {
    echo "Error - file was not uploaded.";
  } else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
      echo "File". basename( $_FILES["fileToUpload"]["name"]). " was uploaded successfully.";

      $to = "szabo@atria.sk";
      $url = $_POST['current_url'];
      $subject = "New image was uploaded";
      $message = "URL:" . $url ;
      $headers = "Gallery" "
" . 'Content-Type: text/plain; charset=UTF-8';
      mail($to,$subject,$message,$headers);
    }
  }

?>

Is there an error in my code?

</div>
  • 写回答

2条回答 默认 最新

  • douxi5940 2015-06-23 09:54
    关注

    Just to be a little more complete:

    You are missing a so called String Operator ('.') between your two strings:

    $headers = "Gallery" "
    " . 'Content-Type: text/plain; charset=UTF-8';
    

    Should be

    $headers = "Gallery" . "
    " . 'Content-Type: text/plain; charset=UTF-8';
    

    Or

    $headers = "Gallery
    " . 'Content-Type: text/plain; charset=UTF-8';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mysql , 用自己创建的本地主机和用户名 登录不上
  • ¥15 关于#web项目#的问题,请各位专家解答!
  • ¥15 vmtools环境不正常
  • ¥15 请问如何在Ubuntu系统中安装使用microsoft R open?
  • ¥15 jupyter notebook
  • ¥30 informer时间序列预测
  • ¥20 SSR引物多态性分析
  • ¥15 大漠插件在Win11易语言注册调用和免注册灵异事件,VS上注册调用完全没问题
  • ¥15 Addressable缓存机制做热更新的问题
  • ¥15 微信开发者工具vant组件