PHP: How to send file as the attachment without uploading to sever? - php

How to send file by mail() function of PHP as the attachment without uploading file to sever (just after submiting the form, using $_FILES array)?

If you're using the $_FILES array, the file is already uploaded to the server (usually in /tmp), so your question doesn't really make sense.
I understand what you mean, though. You want to send it as an attachment without moving it to a more permanent location. This can be done easily using
file_get_contents($_FILES['attachment']['tmp_name']);.

get the file: $attachment = $_FILES['attachment']['tmp_name'];
get the file name: $attachment_name = $_FILES['attachment']['name'];
add the file to mail: $mail->addAttachment($attachment,$attachment_name);

Related

phpmailer file attachment delivered, but downloads a string only

I have a dxf file saved in my public_html folder on my server. I would like to add this as an attachment to an email. I apply the following code line:
$mail->AddStringAttachment($_SERVER['DOCUMENT_ROOT'] . '/myDxf.dxf', 'myFile.dxf', 'base64', 'application/pdf');
This attaches a dxf and the email is sent. However, when I download the attachment, instead of being a true dxf, it just has a string inside with the file path:
/home3/frank/public_html/myDxf.dxf
Can anyone see what I am doing wrong?
Here are two places to start troubleshooting:
1. Attaching of File
Instead of this:
$mail->AddStringAttachment()
try this:
$mail->AddAttachment()
File Attachments
The command to attach a local file is simply
$mail->addAttachment($path);, where $path contains the path to the
file you want to send, and can be placed anywhere between $mail = new PHPMailer; and sending the message. Note that you cannot use a URL
for the path - you may only use local filesystem path.
If you want to send content from a database or web API (e.g. a remote PDF generator), do not use this method - use addStringAttachment instead.
2. MIME type
Instead of this:
application/pdf
try this:
image/vnd.dxf
List of MIME types: http://www.freeformatter.com/mime-types-list.html

getting file information via url in php

like file upload there are
<?php
$_FILES['file']['tmp_name'];
$_FILES['file']['name'];
$_FILES['file']['size'];
$_FILES['file']['type'];
?>
now.
i have a file that is sitting on my other web server, and i want to get the name size and mime type of that file via url.. is this possible?..
i've alreay tried to use this code below. but it doesn't work
$url = "http://mydomain.com/myfile.rar";
filesize ( $url );
mime_content_type ( $url );
You can try native php function get_headers it's very fast way to read file data
You can't do it like this. The information you get when you use $_FILES is meta-information that is sent along with the file (and the size can even be calculated after the file is retrieved).
You cannot get this information like that, but you can download the actual file and inspect the header information to get that information. To do this, read about curl, which allows you to do HTTP requests to another server.
It might be possible to request just the headers, so you get the information without getting the file, which is obviously more efficient.
Another solution is to implement a file-info script on the other server that allows you to get the file info.
So you could request http://mydomain.com/fileinfo.php?file=myfile.rar. In fileinfo.php you can get all the file info of the given file and just echo it.

PHPMailer giving me "noname" attachments

My problem is exactly like this one, I am getting the attachments without the extension, if I put the extension to the file manually, the file is still not recognized. What could cause this?
This is the attachment-adding part, rest is standard:
foreach($_FILES['uploads']['name'] as $key => $file){
$mail->attachment[] = array($_FILES['uploads']['tmp_name'][$key], $_FILES['uploads']['name'][$key]);
$mail->boundary = "_b" . md5(uniqid(time()));
}
EDIT:
In the beginning of every attachment I get this:
name="phpmailer.inc.php"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="phpmailer.inc.php"
is this normal?
EDIT:
Also, I copied the files to the server and I am adding them to the attachment from there, still, all the files are "noname" and with the correct size.
I think you can not use $_FILES['uploads']['tmp_name'][$key] file as an attachment as $_FILES['uploads']['tmp_name'] are temperory files.
First you need to upload that document/file to the server and than attached it to the mail. Than only it will work.

receive image with PHP sent from FLASH

How can I save image with PHP which was uploaded with http post using FLASH?
To upload to i'm PHP using this:
var upload_to:*=new flash.net.URLRequest("url");
fileHandler.upload(upload_to);
And when I print $_FILES in PHP I get:
{"Filedata":{"name":"IMG_8​658 copy44.jpg","type":"applic​ation\/octet- stream","tmp_​name":"C:\\WINDOWS\\Temp\\​php35.tmp","error":0,"size​":183174}}
so the question is, how to form a file from that $_FILES variable?: ) Thanks
PHP doesn't store the file in memory. It's written out to a temporary file, which you can retrieve the name/path of from the tmp_name value (C:\WINDOWS...). The name field is the filename as provided by the client (IMG_8658...);
In your case, that'd be
$_FILES['Filedata']['tmp_name'] <-- location of temporary file
$_FILES['Filedata']['name'] <---original filename
$_FILES['Filedata']['size'] <--- size in bytes
$_FILES['Filedata']['type'] <-- mime type, as provided by the uploader
$_FILES['Filedata']['error'] <--- error code of upload operation (0 = a-ok)
use copy($_FILES['Filedata']['tmp_name'],'your destination path'); function.

Retrieving a file name to attach to an email with SwiftMailer and PHP

I am able to attach a file using SwiftMailer with its name hardcoded.
But what if the file is uploaded by a user from an HTML form's 'file input type control and has to be sent with an email by a PHP script?
How do I specify the file name in SwiftMailer?
Thank you!
I'm pretty sure in this case you could use the $_FILES['file']['tmp_name'] value. This gives the path to the temporary uploaded file.

Categories