Adding File Upload option

Our customer’s fill out the online form confirming to us that they comply with some industry regulations that are set forth. From there, they are supposed to file out an industry wide form and fax it to us. But, I’m trying to make it easy by simply allowing them to attach the industry form in their attestation of compliance, instead of making it two separate steps for them.

The code you copied in your response was done by someone else, so I have no idea about that. lol

I was trying to fix the code, and now I get “Server Error: The website encountered an error while retreiving (url.php). It may be down for maintenance or configured incorrectly.”

Err. Here’s the php that is “configured incorrectly”
[php]<?php
$subject = 'I am Compliant from ‘.$_POST[‘MID’].’ ';
$emailadd = ‘[email protected]’;
$url = ‘compliance/confirmation.html’;
$req = ‘1’;
$text = “I declare that I have read and understand the Guidelines and that I comply within these guidelines.
:\n\n”;
$space = ’ ';
$line = ’
‘;
foreach ($_POST as $key => $value)
{
if ($req == ‘1’)
{
if ($value == ‘’)
{echo “$key is empty”;die;}
}
$j = strlen($key);
if ($j >= 20)
{echo “Name of form element $key cannot be longer than 20 characters”;die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ’ ‘;}
$value = str_replace(’\n’, “$line”, $value);
$conc = “{$key}:$space{$value}$line”;
$text .= $conc;
$space = ’ ';
}

$file_name = $_FILES[‘userfile’][’’.$_POST[‘MID’].’’];
$random_digit=rand(0000,9999);
$new_file_name = $file_name . $random_digit;
$path= “Confirmation/” . $new_file_name;
if($userfile !=none) {
if(copy($_FILES[‘userfile’][‘tmp_name’], $path)) {
echo “Successful
”;
echo “File Name :” . $new_file_name."
";
echo “File Size :” . $_FILES[‘userfile’][‘size’]."
";
echo “File Type :” . $_FILES[‘userfile’][‘type’]."
";
} else {
echo “Error, file did not upload correctly!”;
}

mail($emailadd, $subject, $text, $header);

echo ‘’;
?>
[/php]

Did i insert the code you provided me in the wrong place (just before the mail command)?

Hmmm, awhile ago I posted the fix for that upload code. But, it seems you can not send two posts by going BACK and entering new info… So, I will retype it all…

First, you changed my code and added some foreign code to change the name of the incoming file from the user. I took that out. I will guess that this “Account Number” is what you want to add to the filename when it is stored on the server. So, I placed it where it belongs in the code. Here is the new version:

[php]

$file_name = $_FILES[‘userfile’];
$new_file_name = $file_name . trim($_POST[‘MID’]);
$path= “Confirmation/” . $new_file_name;
if($userfile) {
if(copy($_FILES[‘userfile’][‘tmp_name’], $path)) {
echo “Successful
”;
echo “File Name :” . $new_file_name."
";
echo “File Size :” . $_FILES[‘userfile’][‘size’]."
";
echo “File Type :” . $_FILES[‘userfile’][‘type’]."
";
} else {
echo “Error, file did not upload correctly!”;
}
[/php]
You also changed the if clause, I fixed it, too…

Try this and let us know…

Getting the same server error as before with this:

[php]<?php
$subject = 'I am Compliant from ‘.$_POST[‘MID’].’ ';
$emailadd = ‘[email protected]’;
$url = ‘compliance/confirmation.html’;
$req = ‘1’;

$text = “I declare that I have read and understand the PGuidelines and that I comply within these guidelines.
:\n\n”;
$space = ’ ';
$line = ’
‘;
foreach ($_POST as $key => $value)
{
if ($req == ‘1’)
{
if ($value == ‘’)
{echo “$key is empty”;die;}
}
$j = strlen($key);
if ($j >= 20)
{echo “Name of form element $key cannot be longer than 20 characters”;die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ’ ‘;}
$value = str_replace(’\n’, “$line”, $value);
$conc = “{$key}:$space{$value}$line”;
$text .= $conc;
$space = ’ ';
}
$file_name = $_FILES[‘userfile’];
$new_file_name = $file_name . trim($_POST[‘MID’]);
$path= “Confirmation/” . $new_file_name;
if($userfile) {
if(copy($_FILES[‘userfile’][‘tmp_name’], $path)) {
echo “Successful
”;
echo “File Name :” . $new_file_name."
";
echo “File Size :” . $_FILES[‘userfile’][‘size’]."
";
echo “File Type :” . $_FILES[‘userfile’][‘type’]."
";
} else {
echo “Error, file did not upload correctly!”;
}

mail($emailadd, $subject, $text, ‘From: ‘.$_POST[‘Name’].’ <’.$_POST[‘Email’].’>’);

echo ‘’;
?>
[/php]

I was trying to fix the code, and now I get "Server Error: The website encountered an error while retreiving (url.php). It may be down for maintenance or configured incorrectly."

Is this upload file named “url.php”? I forget…

I looked back at the original posts and your form contains an ACTION=“sr.php”, not URL.PHP???

So, not sure where the url.php is coming from… Okay, SORRY! Found it…

You are trying to do some sort of a refresh inside PHP:
$url = ‘compliance/confirmation.html’;
echo ‘’;
This should be:
[php]
header(“Location: compliance/confirmation.html”);
[/php]

“url.php” was just may way of giving you the error message without publicly releasing the domain information (since this is a public forum). Sorry if it confused you.

The $url = ‘compliance/confirmation.html’; brings the customer to a confirmation page once they have submitted the form successfully.

For the new line that you say it should be, where do I put that? and should it have a ? before it?

I tried simply replacing the
$url = ‘compliance/confirmation.html’;
with
header(“Location: compliance/confirmation.html”);
but I still get the same error.

The fix for the header/location goes at the end. So, here it is, the full section you posted last. Let me know if it works…
[php]

<?php $subject = 'I am Compliant from '.$_POST['MID'].' '; $emailadd = '[email protected]'; $req = '1'; $text = "I declare that I have read and understand the PGuidelines and that I comply within these guidelines. :\n\n"; $space = ' '; $line = ' '; foreach ($_POST as $key => $value) { if ($req == '1') { if ($value == '') {echo "$key is empty";die;} } $j = strlen($key); if ($j >= 20) {echo "Name of form element $key cannot be longer than 20 characters";die;} $j = 20 - $j; for ($i = 1; $i <= $j; $i++) {$space .= ' ';} $value = str_replace('\n', "$line", $value); $conc = "{$key}:$space{$value}$line"; $text .= $conc; $space = ' '; } $file_name = $_FILES['userfile']; $new_file_name = $file_name . trim($_POST['MID']); $path= "Confirmation/" . $new_file_name; if($userfile) { if(copy($_FILES['userfile']['tmp_name'], $path)) { echo "Successful
"; echo "File Name :" . $new_file_name."
"; echo "File Size :" . $_FILES['userfile']['size']."
"; echo "File Type :" . $_FILES['userfile']['type']."
"; } else { echo "Error, file did not upload correctly!"; } mail($emailadd, $subject, $text, 'From: '.$_POST['Name'].' <'.$_POST['Email'].'>'); header("Location: compliance/confirmation.html"); ?>

[/php]

I think my hair is starting to turn (more) grey! ah!

I’m still getting the server error when I test it on the webpage that the php file is either down for maintenance or improperly configured. Here’s what I have…(again). Please perform a miracle and help me get this working.

[php]<?php

$subject = 'I am Compliant from ‘.$_POST[‘MID’].’ ';
$emailadd = ‘[email protected]’;
$req = ‘1’;

$text = “I declare that I have read and understand the Guidelines and that I comply within these guidelines.
:\n\n”;
$space = ’ ';
$line = ’
‘;
foreach ($_POST as $key => $value)
{
if ($req == ‘1’)
{
if ($value == ‘’)
{echo “$key is empty”;die;}
}
$j = strlen($key);
if ($j >= 20)
{echo “Name of form element $key cannot be longer than 20 characters”;die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ’ ‘;}
$value = str_replace(’\n’, “$line”, $value);
$conc = “{$key}:$space{$value}$line”;
$text .= $conc;
$space = ’ ';
}

$file_name = $_FILES[‘userfile’];
$new_file_name = $file_name . trim($_POST[‘MID’]);
$path= “Confirmation/” . $new_file_name;
if($userfile) {
if(copy($_FILES[‘userfile’][‘tmp_name’], $path)) {
echo “Successful
”;
echo “File Name :” . $new_file_name."
";
echo “File Size :” . $_FILES[‘userfile’][‘size’]."
";
echo “File Type :” . $_FILES[‘userfile’][‘type’]."
";
} else {
echo “Error, file did not upload correctly!”;
}

mail($emailadd, $subject, $text, ‘From: ‘.$_POST[‘Name’].’ <’.$_POST[‘Email’].’>’);

header(“Location: compliance/confirmation.html”);

?>[/php]

@ErnieAlex

Looking over the code, I see the array $userfile as part of the ‘if’ statement that you provided. However, I cannot locate $userfile anywhere else in the code/where this is defined. Am I making sense?

Shouldn’t $userfile be defined somewhere, before we include it as part of the if statement. And, how would I define it since $file_name = $_FILES[‘userfile’] already?
Would it be like this?

$userfile = $_FILES[‘userfile’];
$file_name = $userfile
$new_file_name = $file_name . trim($_POST[‘MID’]);

Please help thanks!

YEAH!! I finally got the dang code to work and upload a file to the server.

BUTTTTT…when I retrieve the pdf file from the server, it does not have a file extension and is damaged when I try to open with Acrobat. How can I define the .pdf extension?

(By the way, great tool for figuring out where the code is wrong. http://www.meandeviation.com/tutorials/learnphp/php-syntax-check/v4/syntax-check.php)

Sorry, I was helping a sick relative and not around much till today! …

Well, in this line:
$new_file_name = $file_name . trim($_POST[‘MID’]);
use this instead…

$new_file_name = $file_name . trim($_POST[‘MID’]) . “pdf”;

Or, if you are going to upload other filetypes, you can decode the type of the file and add it instead of
locking it into just PDF’s… Hope that helps… VERY GLAD YOU figured it all out… Congrats!

Sponsor our Newsletter | Privacy Policy | Terms of Service