/    Sign up×
Community /Pin to ProfileBookmark

sending email attachments via phpmailer

I’m working with phpmailer and trying to create a job app form that allows the user to submit an attachment (their resume) in pdf, doc, or txt format with the rest of the form data.

I found this [URL=”http://zainal.wordpress.com/2007/01/10/sending-email-attachments-in-php-using-phpmailer-class/”]link[/URL] however I’m not sure how to implement the part where you allow the file to be uploaded to the server. Do I need a database? or what do I need to do?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@hastxSep 19.2007 — Creating an upload form can be done very easily. Most of the code involved in many prebuilt scripts is for security, validation, and other peripheral issues.

The basic jist of an upload is:

create the form and point it at the processing script(make shure it is set for multipart/form-data)
[code=html]
<form name="upload" method="POST" action="process_upload.php" enctype="multipart/form-data">
<br>
File to Upload: <input type="file" name="filename" />
<input type="reset" name="reset" value="Reset Form" />
<input type="submit" name="submit" value="submit" />
</form>
[/code]


Next create the processing script
[code=php]
<?
$filename = $_POST['filename']; //this is the filename input of your form
$uploaddir = '/home/user/public_html/hr/uploads/'; //where you want the file to go
$file = basename($_FILES['filename']['name']);
$file = stripslashes($file);

$uploadfile = $uploaddir . $file;

//the move_uploaded_file() function is what moves the temp working file to it's final location.
if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {
echo "<pre>";
echo "<span style="color:green;font-weight:bold;">Successfully Uploaded: $file</span>...n";
} else {
echo "<span style="color:#ad0000;font-weight:bold;">upload failed!</span>n";
}
echo 'Here is some more info on your upload:';
print_r($_FILES);
echo "</pre>";
?>
[/code]


make sure that PHP has write permission to the directory you are uploading to.

This is a very basic upload script....depending on your security and validation needs, you can build the script from these principles and should be able to integrate a script into your site. Most pre-built scripts should be as simple as assigning variables (such as the directory where you want the upload to go), uploading the script to your site, and pointing your form to the script.
Copy linkTweet thisAlerts:
@po3authorSep 19.2007 — make sure that PHP has write permission to the directory you are uploading to.[/QUOTE]


How can I make sure the directory has write permissions?
Copy linkTweet thisAlerts:
@hastxSep 19.2007 — It depends on your host...assuming it is garden variety linux, and just for testing purposes you can just set it to 777. some hosts have a control panel to aid you in that, or you might be able to do it through the FTP client.

once you know everything is working correctly, I would tighten up on that.
Copy linkTweet thisAlerts:
@po3authorSep 20.2007 — I'm getting the following error: What did I do wrong?

[CODE]Notice: Undefined index: filename in C:mydir1mydir2mydir3mydir4testcareerOpptestattach.php on line 2
Successfully Uploaded: php.gif...


Here is some more info on your upload: Array
(
[filename] => Array
(
[name] => php.gif
[type] => image/gif
[tmp_name] => C:PHPuploadtempphpA443.tmp
[error] => 0
[size] => 269
)

)[/CODE]


Here's the php
[code=php]<?
$filename = $_POST['filename']; //this is the filename input of your form
$uploaddir = "c:/mydir1/mydir2/mydir3/mydir4/test/temp/"; //where you want the file to go
$file = basename($_FILES['filename']['name']);
$file = stripslashes($file);

$uploadfile = $uploaddir . $file;

//the move_uploaded_file() function is what moves the temp working file to it's final location.
if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {
echo "<pre>";
echo "<span style="color:green;font-weight:bold;">Successfully Uploaded: $file</span>...n";
} else {
echo "<span style="color:#ad0000;font-weight:bold;">upload failed!</span>n";
}
echo '<br/ ><br /><b>Here is some more info on your upload:</b> ';
print_r($_FILES);
echo "</pre>";
?>[/code]
Copy linkTweet thisAlerts:
@hastxSep 20.2007 — sorry. I copied and pasted from another project...

Notices wont really hurt the function of the program in most cases, so I dont usually display them. To get rid of the notice, comment out, get rid of the line:
[code=php]
$filename = $_POST['filename']; //this is the filename input of your form
[/code]
Copy linkTweet thisAlerts:
@ezigoingOct 31.2007 — Creating an upload form can be done very easily. Most of the code involved in many prebuilt scripts is for security, validation, and other peripheral issues.

The basic jist of an upload is:

create the form and point it at the processing script(make shure it is set for multipart/form-data)
[code=html]
<form name="upload" method="POST" action="process_upload.php" enctype="multipart/form-data">
<br>
File to Upload: <input type="file" name="filename" />
<input type="reset" name="reset" value="Reset Form" />
<input type="submit" name="submit" value="submit" />
</form>
[/code]


Next create the processing script
[code=php]
<?
$filename = $_POST['filename']; //this is the filename input of your form
$uploaddir = '/home/user/public_html/hr/uploads/'; //where you want the file to go
$file = basename($_FILES['filename']['name']);
$file = stripslashes($file);

$uploadfile = $uploaddir . $file;

//the move_uploaded_file() function is what moves the temp working file to it's final location.
if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {
echo "<pre>";
echo "<span style="color:green;font-weight:bold;">Successfully Uploaded: $file</span>...n";
} else {
echo "<span style="color:#ad0000;font-weight:bold;">upload failed!</span>n";
}
echo 'Here is some more info on your upload:';
print_r($_FILES);
echo "</pre>";
?>
[/code]


make sure that PHP has write permission to the directory you are uploading to.

This is a very basic upload script....depending on your security and validation needs, you can build the script from these principles and should be able to integrate a script into your site. Most pre-built scripts should be as simple as assigning variables (such as the directory where you want the upload to go), uploading the script to your site, and pointing your form to the script.[/QUOTE]

I have been searching for a solution to my problem. I do as per this standard approach as as per this stripped down code and it works because when I check the server the file has been uploaded. But the control is handed to the PHP script and I do not know how to prevent that happening as I want the HTML application to stay presnt on the screen.

Demo link http://demo.cubewerx.com.au/NuMaps/test.html

Can you help me please?

[B]At HTML client:[/B]

<form name='form' action="inc/uploadPlotData01.php" enctype="multipart/form-data" method="POST" >

<label class='label'>Enter/Select File for Uploading:</label><br />

<input type="hidden" name="MAX_FILE_SIZE" value="100000" />

<input id="file" class='box' type="file" name="file" value="" size="40" title='locate the file for uploading and plotting on map'/>

<input class='butt' type="submit" value="Upload" />

</form>

[B]The ServerSide PHP:[/B]

<?php

$uploaddir = 'uploads/';

$uploadfile = $uploaddir . basename($_FILES['file']['name']);

move_uploaded_file($_
FILES['file']['tmp_name'], $uploadfile);

?>
×

Success!

Help @po3 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...