...

View Full Version : PHP Code Help Needed



gjcluttrell
01-23-2012, 04:08 PM
The attached code works except the email portion. This code allows a user to upload a file(s) to a network share & has a few check mark boxs for John Doe & Jane Doe. If the box for John or Jane is checked it is suppose to send them an email once submit is pressed but the email does not get sent. Can someone help me out on this? Thanks





<html>
<body>
<body style="background-image:url(FadedBG.png); background-repeat:no-repeat; background-attachment:fixed; background-position:center;">
<body>
</html>

<?php
error_reporting(E_ALL);

date_default_timezone_set('America/Chicago');

// ESTABLISH THE NAME OF THE 'uploads' DIRECTORY
$uploads = 'uploads';

// ESTABLISH THE BIGGEST FILE SIZE WE CAN ACCEPT - ABOUT 8 MB
$max_file_size = '8200000';

// ESTABLISH THE MAXIMUM NUMBER OF FILES WE CAN UPLOAD
$nf = 3;

// ESTABLISH THE KINDS OF FILE EXTENSIONS WE CAN ACCEPT
$file_exts = array
( 'wpd'
, 'docx'
, 'doc'
, 'txt'
, 'pdf'
)
;

// LIST OF THE ERRORS THAT MAY BE REPORTED IN $_FILES[]["error"] (THERE IS NO #5)
$errors = array
( 0 => "Success!"
, 1 => "The file size is over 5MB Please contact IT"
, 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form Please contact IT"
, 3 => "The uploaded file was only partially uploaded Please try again"
, 4 => "No file was uploaded"
, 5 => "UNDEFINED ERROR"
, 6 => "Missing a temporary folder"
, 7 => "Cannot write file to disk"
)
;

// IF THERE IS NOTHING IN $_POST, PUT UP THE FORM FOR INPUT
if (empty($_POST))
{
?>
<h2>You can upload up to <?php echo $nf; ?> file(s) at a time.</h2>


<form name="UploadForm" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />


<p>
Browse to the file(s) you want to upload and click the "Upload" button below.
</p>


<?php // CREATE INPUT STATEMENTS FOR UP TO $n FILE NAMES
for ($n = 0; $n < $nf; $n++)
{
echo "<input name=\"userfile$n\" type=\"file\" size=\"80\" /><br/>\n";
}
?>

<br/>Check this box <input autocomplete="off" type="checkbox" name="overwrite" /> to <strong>OVERWRITE</strong> existing files. If overwritten a backup will be created with a .bak file extension appended to the end of the document.

<?php
if (isset($_POST['formsent'])) {
$destination = array();
if (!empty($_POST['notify_jane'])) $destination[] = 'email1@yahoo.org';
if (!empty($_POST['notify_john'])) $destination[] = 'email2@yahoo.org';
if ($destination) {
mail(join(', ', $destination), 'Notification', 'Hey there');
}
} else {
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="formsent" value="true" />
<br/>Check this box <input autocomplete="off" type="checkbox" name="notify_jane" value="yes" /> to send <strong>John Doe</strong> a notification.
<br/>Check this box <input autocomplete="off" type="checkbox" name="notify_john" value="yes" /> to send <strong>Jane Doe</strong> a notification.
<br>
<input type="submit" value="Submit" />
</form>
<?php
}
die();
}

else
{
// THERE IS POST DATA - PROCESS IT
echo "<h2>Results: File Upload</h2>\n";

// ITERATE OVER THE CONTENTS OF $_FILES
foreach ($_FILES as $my_uploaded_file)
{
// SKIP OVER EMPTY SPOTS - NOTHING UPLOADED
$error_code = $my_uploaded_file["error"];
if ($error_code == 4) continue;

// SYNTHESIZE THE NEW FILE NAME
$f_type = trim(strtolower(end (explode( '.', basename($my_uploaded_file['name'] )))));
$f_name = trim(strtolower(current(explode( '.', basename($my_uploaded_file['name'] )))));
$my_new_file = getcwd() . '/' . $uploads . '/' . $f_name . '.' . $f_type;
$my_file = $uploads . '/' . $f_name . '.' . $f_type;

// OPTIONAL TEST FOR ALLOWABLE EXTENSIONS
if (!in_array($f_type, $file_exts)) die("Sorry, $f_type files not allowed");

// IF THERE ARE ERRORS
if ($error_code != 0)
{
$error_message = $errors[$error_code];
die("Sorry, Upload Error Code: $error_code: $error_message");
}

// GET THE FILE SIZE
$file_size = number_format($my_uploaded_file["size"]);

// IF THE FILE IS NEW (DOES NOT EXIST)
if (!file_exists($my_new_file))
{
// IF THE MOVE FUNCTION WORKED CORRECTLY
if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
{
$upload_success = 1;
}
// IF THE MOVE FUNCTION FAILED
else
{
$upload_success = -1;
}
}

// IF THE FILE ALREADY EXISTS
else
{
echo "<br/><b><i>$my_file</i></b> already exists.\n";

// SHOULD WE OVERWRITE THE FILE? IF NOT
if (empty($_POST["overwrite"]))
{
$upload_success = 0;
}
// IF WE SHOULD OVERWRITE THE FILE, TRY TO MAKE A BACKUP
else
{
$now = date('Y-m-d');
$my_bak = $my_new_file . '.' . $now . '.bak';
if (!copy($my_new_file, $my_bak))
{
echo "<br/><strong>Attempted Backup Failed!</strong>\n";
}
if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
{
$upload_success = 2;
}
else
{
$upload_success = -1;
}
}
}

// REPORT OUR SUCCESS OR FAILURE
if ($upload_success == 2) { echo "<br/>It has been overwritten.\n"; }
if ($upload_success == 1) { echo "<br/><strong>$my_file</strong> has been saved.\n"; }
if ($upload_success == 0) { echo "<br/><strong>It was NOT overwritten.</strong>\n"; }
if ($upload_success < 0) { echo "<br/><strong>ERROR: $my_file NOT SAVED - SEE WARNING FROM move_uploaded_file() COMMAND</strong>\n"; }
if ($upload_success > 0)
{
echo "$file_size bytes uploaded.\n";
if (!chmod ($my_new_file, 0755))
{
echo "<br/>chmod(0755) FAILED: fileperms() = ";
echo substr(sprintf('%o', fileperms($my_new_file)), -4);
}
echo "<br/><a target=\"_blank\" href=\"$my_file\">See the file $my_file</a>\n";

}

}
}

kyle0032
01-23-2012, 05:19 PM
I am not sure if is intentional or not but this sends no from headers...


if (isset($_POST['formsent'])) {
$destination = array();
if (!empty($_POST['notify_jane'])) $destination[] = 'email1@yahoo.org';
if (!empty($_POST['notify_john'])) $destination[] = 'email2@yahoo.org';
if ($destination) {
mail(join(', ', $destination), 'Notification', 'Hey there');
}
} else {

Try this:


$mailheaders = "From: My Website<info@mywebsite.ca>";
$destination = array();
if (!empty($_POST['notify_jane'])) $destination[] = 'email1@yahoo.org';
if (!empty($_POST['notify_john'])) $destination[] = 'email2@yahoo.org';
if ($destination) {
mail(join(', ', $destination), 'Notification', 'Hey there', $mailheaders);
}


If that does not work I would try separating the e-mails by ; instead of ,.. If that does not help use a while loop to go through the destination array elements and use multiple mail() commands for each e-mail address.

Let me know if this helps.

Kyle

gjcluttrell
01-23-2012, 05:40 PM
To simplify things & have placed the code below on it's own page:



<?php
$mailheaders = "From: My Website<info@mywebsite.ca>";
$destination = array();
if (!empty($_POST['notify_jane'])) $destination[] = 'email1@yahoo.org';
if ($destination) {
mail(join(', ', $destination), 'Notification', 'Hey there', $mailheaders);
}

?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="formsent" value="true" />
<br/>Check this box <input autocomplete="off" type="checkbox" name="notify_jane" value="yes" /> to send <strong>John Doe</strong> a notification.
<input type="submit" value="Submit" />
</form>
<?php


If I go to that page I see "
Check this box to send John Doe a notification" with a submit button. If I put a check mark in the check box & click the submit button the URL changes to http://myserver/flow/%3C?=$_SERVER['PHP_SELF']?>

and still no email...thanks again for your help on this.

gjcluttrell
01-23-2012, 05:46 PM
I got it....I changed this line:

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">

to

<form method="post">



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum