The following script sends a file to a given email along with a message.
I am posting the file to the script as a query string, eg. I link to script like this:
<a href="php/sendfile.php?remote=sendfile/abc.pdf">receive pdf</a>
My question: How can I return to the original page (the page which links to the script) instead of returning to the script after the file has been sent successfully?
Could header( "Location: be used in combination with javascript:history.go(-1) ?
If so where would it be placed in the script?
Hope that makes sense. Thanks for any help.
<?php
/*
Hack Name: PHP Send File By Email
Hack URI: http://frenchfragfactory.net/ozh/my-projects/php-send-file-by-email-sendmail-attachment/
Description: Fetch a file and send it as an email attachment
Version: 1.0
Author: Ozh
Author URI: http://planetOzh.com
*/
/***********************************
* CONFIGURE THE SCRIPT
* Edit here to suit your needs
***********************************/
$sendfile['email_from'] = "Name <info@name.co.uk>";
// Who the email is from
$sendfile['email_subject'] = "File : %f (%s bytes)";
// The Subject of the email (%f will be file name, %s size)
$sendfile['email_message'] = "File name : %f\nSize: %s bytes\nDownloaded from : %l\n\n%t";
// Message along with attachment (%f: file name, %s: size, %l: original location, %t: free text)
$sendfile['email_to'] = "info@host.co.uk";
// Default value of recipient's email when nothing specified
$sendfile['file_dir'] = "/home/lokust/public_html/sendfile_temp/";
// Directory in which sent files will be mirrored first. Create this directory and chmod it 777 (make it writeable)
$sendfile['file_delete'] = 1;
// Default behaviour of the script once the file has been sent by email. Delete it (1) or keep it on server (0)
$sendfile['file_log'] = "sendfile.log";
// Log file in which dates and URL of files sent are stored
/***********************************
* END CONFIG
* Do not modify below, unless you know
* what you are doing
***********************************/
$sendfile['remote'] = stripslashes(init('remote'));
$sendfile['local'] = stripslashes(init('local'));
$sendfile['deletefile'] = init('deletefile');
$sendfile['dest'] = init('dest');
$sendfile['text'] = stripslashes(init('text'));
$sendfile['log'] = stripslashes(init('log'));
$sendfile['action'] = init('action');
$sendfile['bintext'] = init('bintext');
html();
if (!$sendfile['remote'] and $sendfile['action']) {
print '<p class="error"><b>ERROR</b> : You did not specify any file to send ! Try again !</p>';
$sendfile['action'] = '';
}
switch ($sendfile['action']) {
case 'mirror':
mirrorfile($sendfile['remote']);
break;
case 'send':
sendfile($sendfile['local']);
default:
printform();
}
function init($in='') {
return @$_GET[$in]?@$_GET[$in]:@$_POST[$in];
}
function logsend() {
global $sendfile;
$log=fopen($sendfile['file_log'],"a");
$stamp = date("Y/m/d G:i:s");
$msg = "$stamp :\n\tFile ${sendfile['remote']}\n\tSent to ${sendfile['dest']}\n\n";
fputs($log,$msg);
fclose($log);
}
function sendfile($input) {
global $sendfile;
/* PREPARE VARIABLES */
if (!$sendfile['dest']) $sendfile['dest'] = $sendfile['email_to'];
if ($sendfile['log']) print $sendfile['log'];
print "<p class=\"info\">Sending file to <b>" . $sendfile['dest'] . "</b> ...</p>";
$sendfile['email_subject'] = str_replace ('%f',basename($input),$sendfile['email_subject']);
$sendfile['email_subject'] = str_replace ('%s',filesize($input),$sendfile['email_subject']);
$sendfile['email_message'] = str_replace ('%f',basename($input),$sendfile['email_message']);
$sendfile['email_message'] = str_replace ('%s',filesize($input),$sendfile['email_message']);
$sendfile['email_message'] = str_replace ('%l',$sendfile['remote'],$sendfile['email_message']);
$sendfile['email_message'] = str_replace ('%t',$sendfile['text'],$sendfile['email_message']);
/* PREPARE MAIL HEADERS */
$headers = "From: ".$sendfile['email_from'];
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
@$sendfile['email_message'] . "\n\n" .
"-- \n" ;
/* PREPARE ATTACHMENT */
$fileatt = basename($input) ;
$fileatt_type = "application/octet-stream";
$file = fopen($input,"r${sendfile['bintext']}");
$data = fread($file,filesize($input));
fclose($file);
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt};\n" .
" name=\"{$fileatt}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
/* SEND FILE */
$ok = @mail($sendfile['dest'], $sendfile['email_subject'], $email_message, $headers);
if($ok) {
echo '<p class="info">File successfully sent!</p>';
logsend();
} else {
echo('<p class="error"><b>ERROR</b> : could not send email.</p>');
}
if($sendfile['deletefile'] and ($input != __FILE__) ) {
$ok=unlink($input);
if ($ok==FALSE) {
echo '<p class="error"><b>ERROR</b> : could not delete file from server.</p>';
} else {
echo '';
}
}
}
function mirrorfile($url) {
global $sendfile;
if (!$sendfile['local']) $sendfile['local'] = basename($url);
$msg = '';
// open source file
$hfici=@fopen($url,"r${sendfile['bintext']}");
// woks fine ?
if($hfici==FALSE){
// No :/
$msg="<b>ERROR</b> : couldn't read source file <b>$url</b>.";
} else {
// Yes : create target file
$hfico=@fopen($sendfile['file_dir'] . '/' . $sendfile['local'],"w${sendfile['bintext']}");
// works fine ?
if($hfico==FALSE){
// No :/
$msg="<b>ERROR</b> : couldn't locally write file";
}
else {
// Yes : read remote and write local
while($buf=fread($hfici,1024)){
fwrite($hfico, $buf);
}
fclose($hfici);
fclose($hfico);
}
}
if ($msg) {
print "<p class='error'>$msg</p>";
printform();
} else {
print '<form method="post" action="'. basename(__FILE__) . '" name="form_send">';
print '<input type="hidden" name="remote" value="' . $sendfile['remote'] . '">';
print '<input type="hidden" name="local" value="' . $sendfile['file_dir'] . '/' . $sendfile['local'] . '">';
print '<input type="hidden" name="deletefile" value="' . $sendfile['deletefile'] . '">';
print '<input type="hidden" name="dest" value="' . $sendfile['dest'] . '">';
print '<input type="hidden" name="text" value="' . $sendfile['text'] . '">';
print '<input type="hidden" name="bintext" value="' . $sendfile['bintext'] . '">';
print '<input type="hidden" name="log" value="' . htmlentities($log . $log2) . '">';
print '<input type="hidden" name="action" value="send">';
//print '<input type="submit">';
print '</form>';
print <<<JS
<script>
document.form_send.submit();
</script>
JS;
}
}
function printform() {
global $sendfile;
if ($sendfile['file_delete']) {
$checked_del = 'checked';
$checked_keep = '';
} else {
$checked_del = '';
$checked_keep = 'checked';
}
$this = basename(__FILE__);
print <<<HTML
</div>
<div id="box">
<fieldset id="form"><legend><a href="$this">Send a File</a></legend>
<form method="post" name="form_mirror">
<input class="wide" type="hidden" name="remote">
<input class="wide" type="hidden" name="local">
<input type="hidden" name="bintext" value="b" id="bintext_b">
<input type="hidden" name="deletefile" value="1" id="radio_del" $checked_del>
<p><span class="label">Send file to</span><input class="wide" type="text" name="dest"></p>
<p><span class="label">Additional message</span><textarea name="text"></textarea></p>
<p><span class="label"> </span><input class="wide" type="submit" value="Send"></p>
<input type="hidden" name="action" value="mirror">
</form>
</fieldset>
</div>
HTML;
}
function html() {
print <<<HTML
<html>
<head>
<title>PHP Send File</title>
</head>
<body>
<link rel="stylesheet" type="text/css" media="all" href="../css/mainII.css">
<div id="log">
HTML;
}
?>