0

Possible Duplicate:
submit success but upload not work at combine form

I am using the code from here from under the title "Submitting only the file". The code is really simple and small, but when I console.log the "file", it doesn't have a location, webkitrelativepath is "", I can see the file name, size etc on submission. I need to post it into my feedback.php file and attach it to an e-mail from there, but I don't know how to take the file out of the formData and upload it to say my uploads folder so I can then e-mail it...

Thanks.

EDIT:

What code I am using at the moment:

var fileInput = document.getElementById('file-upload-field');
var file = fileInput.files[0];
var formData = new FormData();
formData.append('file', file);
console.log(file);

$.ajax({
  url: baseUrl + "feedback.php",
  data: file,
  cache: false,
  contentType: false,
  processData: false,
  type: 'POST',
  success: function(file){
    alert(file);
  }
});

and in my feedback.php

  foreach($_FILES as $file){
    $target_path = "uploads/";
    $target_path = $target_path .basename($file['name']);



if(move_uploaded_file($file['tmp_name'], $target_path)) {
echo "the file ".basename($file['name'])." has been uploaded";
}
else {
 echo "there was an error";
}
$mail->AddAttachment($target_path);

}

This doesn't upload or attach any files anywhere...

Community
  • 1
  • 1
  • What is this post about then: http://blog.new-bamboo.co.uk/2012/01/10/ridiculously-simple-ajax-uploads-with-formdata ? –  Jan 07 '13 at 10:37
  • Okay, changing my comments, sec. Didn't notice you were expressly using the File API. – Charles Jan 07 '13 at 10:39
  • So, files uploaded in that way *don't show up in `$_FILES`*. You need to read `stdin` or view `$HTTP_RAW_POST_DATA`, if it's defined. This means you also don't get the MIME type or original user file name. I *urge* you to use a third-party upload widget for this task. – Charles Jan 07 '13 at 10:41
  • @SEROK: Please contact the author of that tutorial and ask for PHP example code. We can not give support for any tutorial out there online, first contact the original author if you run into problems with such a tutorial. – hakre Jan 07 '13 at 10:51

1 Answers1

0

Just upload the file with

move_uploaded_file(string $filename , string $destination );

and than mail as an attachment like below:

require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->Host = 'smtp_host';      
$mail->Port = 'smtp_port';
$mail->SMTPAuth = true;         
$mail->Username = 'smtp_user';  // SMTP username
$mail->Password = 'smtp_pass';  // SMTP password
$mail->From = "from@abc.ocm";
$mail->FromName = "fromName";
$mail->AddAddress($email);
$mail->Subject = "Your subject line";
$mail->AddAttachment($source, $fileName);
$mail->Body = "Your Message";

// Send the email.
if(!$mail->Send())
    $message = "Displya some error.";

}

Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90