Page 1 of 1

Unable to upload File to specified location

Posted: Fri Oct 31, 2008 3:12 pm
by njmeyer
All,

    I am trying to use the form below to sent and email to a specified person and the user filling out the form needs to have the ability to attach a document, specifically a PDF.

    I have been working on this issue for a day or two now and I am not getting any results.  I do not think that the attached file is showing up at all.  When the form is submitted and I echo the attributes for $HTTP_POST_FILE['attachment']['name'] nothing shows up.  I think that everything else should work.  I cannot get the attached file to do a darn thing.  Any help is appreciated.

Using CMS 1.2 Barbados

    I have also tried using just the $_FILES attribute as well as using cms_move_uploaded_file and neither of them have worked.

My code thus far:

Code: Select all

function smarty_cms_function_test($params, &$smarty) {

require ($_SERVER['DOCUMENT_ROOT'] . '/icx_custom_functions.php');
	
	global $gCms;
	
	if (empty($params['email'])) {
		echo '<p class="warning">An email address must be specified in order to use this plugin.</p>';
		return;
	}
	else {
		$to = $params['email'];
	}

	$errors = $name = $email = $phone = $company = $subject = $referral = $message = '';
	
	// If the form has been submitted.
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	
		// Set variables.
		$subject = "CNC Quote Request";
		
		if (!empty($_POST['name'])) {
			$name = email_clean($_POST['name']);
		}
		if (!empty($_POST['email'])) {
			$email = email_clean($_POST['email']);
		}
		if (!empty($_POST['phone'])) {
			$phone = email_clean($_POST['phone']);
		}
		if (!empty($_POST['date'])) {
			$date = email_clean($_POST['date']);
		}
		if (!empty($_POST['BU'])) {
			$BU = email_clean($_POST['BU']);
		}
		if (!empty($_POST['type'])) {
			$type = email_clean($_POST['type']);
		}
		if (!empty($_POST['message'])) {
			$message = email_clean($_POST['message']);
		}
	
if (empty($name)) {
			$errors .= "\t\t\t" . '<li>' . 'Please enter your name.' . '</li>' . "\n";
		}
		if (empty($email)) {
			$errors .= "\t\t\t" . '<li>' . 'Please enter your email address.' . '</li>' . "\n";
		}
		elseif (!valid_email($email)) {
			$errors .= "\t\t\t" . '<li>' . 'Your email address is not valid.' . '</li>' . "\n";
		}
		if ($subject == "None") {
			$errors .= "\t\t\t" . '<li>' . 'Please choose a subject.' . '</li>' . "\n";
		}
		if (empty($date)) {
			$errors .= "\t\t\t" . '<li>' . 'Please enter your due date.' . '</li>' . "\n";
		}
		if ($referral == "None") {
			$errors .= "\t\t\t" . '<li>' . 'Please choose a referral option.' . '</li>' . "\n";
		}
		if (empty($message)) {
			$errors .= "\t\t\t" . '<li>' . 'Please enter a message.' . '</li>' . "\n";
		}
		else {
			$message .= "\r\n\r\nQuote Type:$type\r\n\r\nDue Date:$date\r\n\r\n$name\r\n$BU\r\n$email\r\n$phone\r\n";
		}
echo $HTTP_POST_FILES["attachment"]["tmp_name"];
$email_from = $name; // Who the email is from 
$email_subject = "CNC Quote Request"; // The Subject of the email 
$email_message = $message; // Message that the email has in it 

$email_to = $to; // Who the email is too 

$headers = "From: ".$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/html; charset=\"iso-8859-1\"\n" . 
               "Content-Transfer-Encoding: 7bit\n\n" . 
$email_message . "\n\n"; 



/* First File */ 
echo $_SERVER['DOCUMENT_ROOT'] . '/tmp/' . $HTTP_POST_FILES['attachment']['name'];
echo $HTTP_POST_FILES['attachment']['tmp_name'];
echo $HTTP_POST_FILES['attachment']['name'];
    if( (isset($HTTP_POST_FILES['attachment'])) && (is_uploaded_file($HTTP_POST_FILES['attachment']['tmp_name'])) )
{

 $filepath = $_SERVER['DOCUMENT_ROOT'] . '/tmp/' . $HTTP_POST_FILES['attachment']['name'];
 move_uploaded_file($HTTP_POST_FILES['attachment']['tmp_name'], $filepath);
}

	$fileatt = $filepath; // Path to the file                  
	$fileatt_type = $HTTP_POST_FILES['attachment']['type']; // File Type 
	$fileatt_name = $HTTP_POST_FILE['attachment']['name']; // Filename that will be used for the file as the attachment
/*
$uploaddir = "temp/"; // Relative path under webroot
$uploadfile = $uploaddir . basename($_FILES['attachment']['name']);

if (move_uploaded_file($_FILES['attachment']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "File uploading failed.\n";
}*/

$file = fopen($fileatt,'rb'); 
$data = fread($file,filesize($fileatt)); 
fclose($file); 


$data = chunk_split(base64_encode($data)); 

$email_message .= "--{$mime_boundary}\n" . 
                  "Content-Type: {$fileatt_type};\n" . 
                  " name=\"{$fileatt_name}\"\n" . 
                  //"Content-Disposition: attachment;\n" . 
                  //" filename=\"{$fileatt_name}\"\n" . 
                  "Content-Transfer-Encoding: base64\n\n" . 
                 $data . "\n\n" . 
                  "--{$mime_boundary}\n"; 
unset($data); 
unset($file);
unset($fileatt); 
unset($fileatt_type); 
unset($fileatt_name); 

/* End of File Config */ 

// To add more files just copy the file section again, but make sure they are all one after the other! If they are not it will not work! 


$ok = @mail($email_to, $email_subject, $email_message, $headers); 

if($ok) { 
echo "<font face=verdana size=2>The file was successfully sent!</font>"; 
} else { 
die("Sorry but the email could not be sent. Please go back and try again!"); 
} 
}
?>
<form action="<?php echo form_action(); ?>" method="post" id="contact" ENCTYPE="multipart/formdata">
	<div class="select clearfix">
		<label for="BU">Business Unit <abbr title="Required">*</abbr></label>
		<select name="BU" id="BU">
			<option <?php if($BU == "360 Surveillance") echo 'selected="selected" '; ?>value="360 Surveillance">360 Surveillance</option>
			<option <?php if($BU == "ICx Agentase") echo 'selected="selected" '; ?>value="ICx Agentase">ICx Agentase</option>
			<option <?php if($BU == "ICx Biodefense") echo 'selected="selected" '; ?>value="ICx Biodefense">ICx Biodefense</option>
			<option <?php if($BU == "ICx Biosystems") echo 'selected="selected" '; ?>value="ICx Biosystems">ICx Biosystems</option>
			<option <?php if($BU == "ICx Control Systems") echo 'selected="selected" '; ?>value="ICx Control Systems">ICx Control Systems</option>
			<option <?php if($BU == "ICx Crypgenics") echo 'selected="selected" '; ?>value="ICx Crypgenics">ICx Crypgenics</option>
			<option <?php if($BU == "ICx Griffin Analytical") echo 'selected="selected" '; ?>value="ICx Griffin Analytical">ICx Griffin Analytical</option>
			<option <?php if($BU == "ICx Imaging Systems") echo 'selected="selected" '; ?>value="ICx Imaging Systems">ICx Imaging Systems</option>
			<option <?php if($BU == "ICx Nomadics") echo 'selected="selected" '; ?>value="ICx Nomadics">ICx Nomadics</option>
			<option <?php if($BU == "ICx Photonics") echo 'selected="selected" '; ?>value="ICx Photonics">ICx Photonics</option>
			<option <?php if($BU == "ICx Radar Systems") echo 'selected="selected" '; ?>value="ICx Radar Systems">ICx Radar Systems</option>
			<option <?php if($BU == "ICx Radiations") echo 'selected="selected" '; ?>value="ICx Radiations">ICx Radiations</option>			
			<option <?php if($BU == "ICx Tactical Platforms") echo 'selected="selected" '; ?>value="ICx Tactical Platforms">ICx Tactical Platforms</option>
			<option <?php if($BU == "ICx Transportation") echo 'selected="selected" '; ?>value="ICx Transportation">ICx Transportation</option>			
			<option <?php if($BU == "ICx Vision Systems") echo 'selected="selected" '; ?>value="ICx Vision Systems">ICx Vision Systems</option>
			<option <?php if($BU == "PBA Engineering") echo 'selected="selected" '; ?>value="PBA Engineering">PBA Engineering</option>
		</select>
	</div>
	<div class="text clearfix">
		<label for="name">Name <abbr title="Required">*</abbr></label>
		<input type="text" id="name" name="name" value="<?php echo $name; ?>" />
	</div>
	<div class="text clearfix">
		<label for="email">Email <abbr title="Required">*</abbr></label>
		<input type="text" id="email" name="email" value="<?php echo $email; ?>" />
	</div>
	<div class="text clearfix">
		<label for="phone">Phone</label>
		<input type="text" id="phone" name="phone" value="<?php echo $phone; ?>" />
	</div>
	<div class="radios clearfix">
		<label for="type">Quote Type</label><br/>
		<input type="radio" name="type" value="Production">Production<br/>
		<input type="radio" name="type" value="IRAD" checked>IRAD<br/>
		<input type="radio" name="type" value="CRAD">CRAD
	</div>
	<div class="text clearfix">
		<label for="date">Date needed</label>
		<input type="text" id="date" name="date" value="<?php echo $date; ?>" />
	</div>
	<div class="text clearfix">
		<label for="attachment">Attach a File</label>
		<input type="file" id="attachment" name="attachment"/>
	</div>
	<div class="text clearfix">
		<label for="message">Message <abbr title="Required">*</abbr></label>
		<textarea id="message" name="message" rows="12" cols="48"><?php echo $message; ?></textarea>
	</div>
	<div class="buttons clearfix">
		<input type="submit" class="button" value="Submit" /> 
		<input type="reset"  class="button" value="Clear" />
	</div>
</form>
<?php

} 
Thanks,
njmeyer

Re: Unable to upload File to specified location

Posted: Mon Dec 15, 2008 7:15 pm
by DirtyMusic
I have the same problem using $HTTP_POST_FILES :(

Re: Unable to upload File to specified location

Posted: Thu Feb 26, 2009 5:24 pm
by njmeyer
This is the code that I used to have someone upload a file to a location of the sever.  Just place it in a user defined tag.  The put that tag in the page that you wish and you should be all set to go.  I would recommend that you check the file type of the file that is being uploaded, to make sure that it is not dangerous.

Code: Select all


if ($_SERVER['REQUEST_METHOD'] == 'POST') {

//Set path
$path = 'uploads/file/.../';


if (file_exists($_FILES['upload']['tmp_name'])){
$file= basename($_FILES['upload']['name']);    
$file_target = $path.$file;
if(move_uploaded_file($_FILES['upload']['tmp_name'], $file_target)){

echo '<div class="success">Your file was successfully uploaded.</div>' . "\n";
}
else{
echo '<div class="warning">Your file was not uploaded.</div>' . "\n";
}
}
else{
echo '<div class="warning">Please choose a file to upload</div>' . "\n";
}

}

?>
<form action="<?php echo icx_form_action(); ?>" method="post" enctype="multipart/form-data" name ="update" id="uploads">
	<fieldset>
		<h3 style="padding: 3px 0 0 5px">Upload a File</h3>
		<div class="text clearfix">
			<label for="upload">File to Upload</label>
			<input type="file" name="upload" id="upload" >
		</div>
<div class="buttons">
			<input name="submit" type="submit" value="Submit" />
		</div>
                </fieldset>
	</form>
<?php

Regards,
njmeyer