954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sending attached files in php

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!

3DProf4online
Light Poster
41 posts since Jan 2007
Reputation Points: 10
Solved Threads: 1
 

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.

Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 

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

R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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>";
	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>";
	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>";
	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:
	<input type="file" name="upload_file" size="50" />
	
	<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.

mexabet
Junior Poster
148 posts since Mar 2008
Reputation Points: 16
Solved Threads: 9
 

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>";
	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>";
	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>";
	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:
	<input type="file" name="upload_file" size="50" />
	
	<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..

helpless_101
Newbie Poster
5 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

3DProf4online
Light Poster
41 posts since Jan 2007
Reputation Points: 10
Solved Threads: 1
 

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>";
	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>";
	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>";
	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:
	<input type="file" name="upload_file" size="50" />
	
	<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['upload_file']['type'] 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['upload_file']['name'] 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
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

R0bb0b,

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

mexabet
Junior Poster
148 posts since Mar 2008
Reputation Points: 16
Solved Threads: 9
 

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.

R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

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!

3DProf4online
Light Poster
41 posts since Jan 2007
Reputation Points: 10
Solved Threads: 1
 

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.

Attachments class.email_.php (6.59KB)
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

This is possible but I think you have to pay!

viksan
Newbie Poster
4 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

viksan
Newbie Poster
4 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

AyuBan
Newbie Poster
1 post since May 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You