Could you please help realize the procedure of sending attached files in php. If it is possible, please, provide me with ready code examples. Thanks in advance!

Recommended Answers

All 14 Replies

There are multiple resources online which deal with this issue, a quick search comes up with exactly what you are looking for, please research and dont ask other people to do all the work.

Post an example of what you have if it isnt working and we can assist you.

phpmailer is an amazing class and there are ready examples in the download, just do a search for phpmailer download.

See if this helps. To use this script, you need to create a folder named uploads where the uploaded files would be stored.

<?php
$maxsize=28480; // Set the maximum upload size in bytes
if (!$_POST['submit']) {
    //print_r($_FILES);
	$error=" ";
	// This will cause the rest of the process to be skipped
	//and the upload form displays
}
if (!is_uploaded_file($_FILES['upload_file']['tmp_name']) AND
!isset($error)) {
    $error = "<b>You must upload a file!</b><br /><br />";
	unlink($_FILES['upload_file']['tmp_name']);
}
if ($_FILES['upload_file']['size'] > $maxsize AND !isset($error)) {
    $error = "<b>Error, file must be less than $maxsize bytes.</b><br /><br />";
	unlink($_FILES['upload_file']['tmp_name']);
}
if($_FILES['upload_file']['type'] != "image/gif" AND
$_FILES['upload_file']['type'] != "image/pjpeg" AND
$_FILES['upload_file']['type'] !="image/jpeg" AND !isset($error)) {
    $error = "<b>You may only upload .gif or .jpeg files.<b><br /><br />";
	unlink($_FILES['upload_file']['tmp_name']);
}
if (!isset($error)) {
    move_uploaded_file($_FILES['upload_file']['tmp_name'],
	                   "uploads/".$_FILES['upload_file']['name']);
	print "Thank you for your upload.";
	exit;
}
else
{
    echo ("$error");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP File Upload Script</title>
</head>

<body>
<form action="<?php echo(htmlspecialchars($_SERVER['PHP_SELF']))?>" method="post"
enctype="multipart/form-data">
    Choose a file to upload:<br />
	<input type="file" name="upload_file" size="50" />
	<br />
	<input type="submit" name="submit" value="Submit" />
	<input type="reset" name="Reset" value="Reset" />
</form>
</body>
</html>

This bit of code moves the uploaded file from a temporary directory into the uploads directory:

move_uploaded_file($_FILES['upload_file']['tmp_name'],
	                   "uploads/".$_FILES['upload_file']['name']);

The MIME types included here are: .gif, .pjpeg and .jpeg but you may like to add more file formats.
This first line of the code defines the maximum file size:

$maxsize=28480; // Set the maximum upload size in bytes

You can edit the size to suit your needs.

commented: Good Work Friend!!!! +2

See if this helps. To use this script, you need to create a folder named uploads where the uploaded files would be stored.

<?php
$maxsize=28480; // Set the maximum upload size in bytes
if (!$_POST['submit']) {
    //print_r($_FILES);
	$error=" ";
	// This will cause the rest of the process to be skipped
	//and the upload form displays
}
if (!is_uploaded_file($_FILES['upload_file']['tmp_name']) AND
!isset($error)) {
    $error = "<b>You must upload a file!</b><br /><br />";
	unlink($_FILES['upload_file']['tmp_name']);
}
if ($_FILES['upload_file']['size'] > $maxsize AND !isset($error)) {
    $error = "<b>Error, file must be less than $maxsize bytes.</b><br /><br />";
	unlink($_FILES['upload_file']['tmp_name']);
}
if($_FILES['upload_file']['type'] != "image/gif" AND
$_FILES['upload_file']['type'] != "image/pjpeg" AND
$_FILES['upload_file']['type'] !="image/jpeg" AND !isset($error)) {
    $error = "<b>You may only upload .gif or .jpeg files.<b><br /><br />";
	unlink($_FILES['upload_file']['tmp_name']);
}
if (!isset($error)) {
    move_uploaded_file($_FILES['upload_file']['tmp_name'],
	                   "uploads/".$_FILES['upload_file']['name']);
	print "Thank you for your upload.";
	exit;
}
else
{
    echo ("$error");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP File Upload Script</title>
</head>

<body>
<form action="<?php echo(htmlspecialchars($_SERVER['PHP_SELF']))?>" method="post"
enctype="multipart/form-data">
    Choose a file to upload:<br />
	<input type="file" name="upload_file" size="50" />
	<br />
	<input type="submit" name="submit" value="Submit" />
	<input type="reset" name="Reset" value="Reset" />
</form>
</body>
</html>

This bit of code moves the uploaded file from a temporary directory into the uploads directory:

move_uploaded_file($_FILES['upload_file']['tmp_name'],
	                   "uploads/".$_FILES['upload_file']['name']);

The MIME types included here are: .gif, .pjpeg and .jpeg but you may like to add more file formats.
This first line of the code defines the maximum file size:

$maxsize=28480; // Set the maximum upload size in bytes

You can edit the size to suit your needs.

hi.. does this code displays the uploaded picture? tnx..

Thanks, mexabet, your post seems to be the most helpful for me! :)

See if this helps. To use this script, you need to create a folder named uploads where the uploaded files would be stored.

<?php
$maxsize=28480; // Set the maximum upload size in bytes
if (!$_POST['submit']) {
    //print_r($_FILES);
	$error=" ";
	// This will cause the rest of the process to be skipped
	//and the upload form displays
}
if (!is_uploaded_file($_FILES['upload_file']['tmp_name']) AND
!isset($error)) {
    $error = "<b>You must upload a file!</b><br /><br />";
	unlink($_FILES['upload_file']['tmp_name']);
}
if ($_FILES['upload_file']['size'] > $maxsize AND !isset($error)) {
    $error = "<b>Error, file must be less than $maxsize bytes.</b><br /><br />";
	unlink($_FILES['upload_file']['tmp_name']);
}
if($_FILES['upload_file']['type'] != "image/gif" AND
$_FILES['upload_file']['type'] != "image/pjpeg" AND
$_FILES['upload_file']['type'] !="image/jpeg" AND !isset($error)) {
    $error = "<b>You may only upload .gif or .jpeg files.<b><br /><br />";
	unlink($_FILES['upload_file']['tmp_name']);
}
if (!isset($error)) {
    move_uploaded_file($_FILES['upload_file']['tmp_name'],
	                   "uploads/".$_FILES['upload_file']['name']);
	print "Thank you for your upload.";
	exit;
}
else
{
    echo ("$error");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP File Upload Script</title>
</head>

<body>
<form action="<?php echo(htmlspecialchars($_SERVER['PHP_SELF']))?>" method="post"
enctype="multipart/form-data">
    Choose a file to upload:<br />
	<input type="file" name="upload_file" size="50" />
	<br />
	<input type="submit" name="submit" value="Submit" />
	<input type="reset" name="Reset" value="Reset" />
</form>
</body>
</html>

This bit of code moves the uploaded file from a temporary directory into the uploads directory:

move_uploaded_file($_FILES['upload_file']['tmp_name'],
	                   "uploads/".$_FILES['upload_file']['name']);

The MIME types included here are: .gif, .pjpeg and .jpeg but you may like to add more file formats.
This first line of the code defines the maximum file size:

$maxsize=28480; // Set the maximum upload size in bytes

You can edit the size to suit your needs.

Thanks, mexabet, your post seems to be the most helpful for me! :)

Sorry to rain on your parade, but you should never go by $_FILES as a security check since this is just a line in the header and can easily be reproduced manually. The browser doesn't even have to provide this info to php so sometimes you won't even be given this info. I could very easily send a php file with the header of "image/gif" or if this is a mail script I could send a virus or what ever I wanted to. You should always parse $_FILES and check the extension that way. This is how I would go about verifying the extension of the filename.

<?php
function returnFileName($file)
{
	$dot = strrpos($file, '.');
	if($dot === false)//file has no dot
	{
		return false;
	}
	
	$fileinfo = array();
	$fileinfo['base'] = substr($file, 0, $dot);
	$fileinfo['ext'] = strtolower(substr($file, $dot + 1));
	return $fileinfo;
} 

$allowedext = array("jpg", "gif");
$filearray = returnFileName(basename($_FILES['upload_file']['name']));

if($filearray == false || !isset($filearray['ext']) || !in_array($filearray['ext'], $allowedext))
{
	$error .= "invalid file type";
}
?>

R0bb0b,

No offenses taken! Instead, we are working together to get a good working script for our fellow community member.

commented: indeed :) +2

R0bb0b,

No offenses taken! Instead, we are working together to get a good working script for our fellow community member.

And the opportunity to bring into the open and alert countless others of a security misconception that apparently often goes unnoticed.

Thanks guys for all your advices! I will invite one guy today, he is a good programmer. He knows PHP language better than I do :) I will show him this post and we will consider all your tips and code examples in detail! Thanks for your help!

Since you seem to have things figured out for the most part this may not be of use to you. I needed a good email class for my projects, so I made one. Its attached.

All you have to do to add uploaded attachments, is use a form with input file fields. the script will load them automatically. you don't have to transfer them to the server first.

you can also use files on the server.

An example how to use it:

<?php

require_once( 'class.email.php' );

$email = new emailHandler();

$email->uploadedFiles = true; //Set to false if you do not want uploaded files and only want to send files already on the server.

//if uploaded files is true then make sure you set the allowed file extensions and the maxsize

$email->allowedFiles = array( 'php','doc','ect...' );
$email->maxSize = 121354; //whatever you want.

$email->from( 'YourWebsite.com','info@yourwebsite.com' ); //set who the email is from.

$email->to( 'test@test.com' ); //you can have as many to's as you want
$email->to( 'someone@somewhere.net' );
$email->cc( 'asdf@asdf.com' ); //you can use carbon copies
$email->bcc( 'asdfadsf@asdkfj.com' ); //you can also use bind carbon copies

$email->subject( 'testing' ); //sets the subject

$type = 'text'; // can be set to 'html' if you want to send html emails.

//to add attachments from server, use:
$email->addAttachment( 'path/to/file.ext' );

$message = "Hello,\n\nWelcome to our site!";
$email->message( $message,$type );

$email->sendMail(); //sends the email(s)

?>

It works fine for me. It could use a little improvement, but that will come when I have some more time to mess with it.

This is possible but I think you have to pay!

commented: People shouldn't have to pay to get help on this site. It defeats the main purpose of the w \ebsite. +0

I know how can help you ,but maybe have to pay for help.
e-mail : [email removed]

This message is for KKeith29

Hi thr, I looked at your php script but I didnt understand some code. I am looking for a code that can send online form with attachment (ONLY ONE zip file) by email. I read your thread and it seems your code works. But thr is some line of code that I think I dont need thm. If you could please help solve my problem. I can send the form by email but i only need the attachment part. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.