0

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" "\r\n" . 'Content-Type: text/plain; charset=UTF-8';
      mail($to,$subject,$message,$headers);
    }
  }

?>

Is there an error in my code?

moffeltje
  • 4,521
  • 4
  • 33
  • 57
freewheeler
  • 1,306
  • 2
  • 12
  • 24

2 Answers2

1

A missing dot after "Gallery":

$headers = "Gallery" . "\r\n" . 'Content-Type: text/plain; charset=UTF-8';
freewheeler
  • 1,306
  • 2
  • 12
  • 24
Tim
  • 143
  • 2
  • 12
1

Just to be a little more complete:

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

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

Should be

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

Or

$headers = "Gallery\r\n" . 'Content-Type: text/plain; charset=UTF-8';
moffeltje
  • 4,521
  • 4
  • 33
  • 57