Page 1 of 1

Posted: Thu Oct 26, 2006 1:35 pm
by PattyB
I have tried uploading the files using the below mentioned technique however when I open the uploaded file this is all it contains:

C:\WINDOWS\TEMP\php6E.tmp

Where the .tmp file will change values.

Do you know what could be causing this?

d11wtq wrote:
jayshields wrote:I've never sent an email with an attachment via PHP. It's becoming confusing for me. I decided to use Swift, but I can't find any attachment examples where it sends an email with a user uploaded file; they are always server side files.

Does that mean I have to upload the file, add it as attachment, send the email, then delete the uploaded file? I'm not 100% sure how attachments work anyways - would I have to wait until the user has recieved the email and grabbed the attachment before deleting the uploaded file?

Thanks.
Upload the file, using $_FILES['form_field_name']['tmp_name'] as the path:

Code: Select all

$swift->addAttachment($_FILES['foo']['tmp_name'], $_FILES['foo']['name'], $_FILES['foo']['type']);
PHP should remove temporary files itself if I remember correctly... you'd need to use move_uploaded_file() if you wanted to keep a copy on the server.

Posted: Thu Oct 26, 2006 1:55 pm
by RobertGonzalez
This was split from the SwiftMailer Code Snippet thread.

Posted: Thu Oct 26, 2006 5:19 pm
by Chris Corbyn
You're passing the path to the file, when you should be passing the file's contents. Use file_get_contents()...

Code: Select all

$swift->addAttachment(
    file_get_contents($_FILES['foo']['tmp_name']), $_FILES['foo']['name'], $_FILES['foo']['type']);

Posted: Thu Oct 26, 2006 5:45 pm
by PattyB
d11wtq wrote:You're passing the path to the file, when you should be passing the file's contents. Use file_get_contents()...

Code: Select all

$swift->addAttachment(
    file_get_contents($_FILES['foo']['tmp_name']), $_FILES['foo']['name'], $_FILES['foo']['type']);
Yes, that was it. Thanks.

Posted: Thu Oct 26, 2006 5:47 pm
by Chris Corbyn
Glad you got it sorted. I have to apologise since re-reading your post I see it was me who first provided such code. My bad :oops: