...

View Full Version : upload doc with form



grudz
09-10-2005, 03:32 AM
Hi,

I have an upload script that allow the user to upload either a .pdf or a .doc. I also have a form where the user will enter information such as his name, email, etc....

My problem, however, is that I can't combine both. Basically, I want the user to fill out the textboxes, then click on "Browse" and select the file he wants to upload, then presses submit.

If some of the text boxes aren't filled in correctly or if the file he tries to upload is invalid, the user will get an error message. I can get them to work seperatly, but not together.

here's the link so you can view how it looks link (http://www.dkaygraphix.com/zefsci/en/home.php?id=contactopp)

and here is the code for the upload



//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// You may change maxsize, and allowable upload file types.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//Mmaximum file size. You may increase or decrease.
$MAX_SIZE = 75000;

//Allowable file Mime Types. Add more mime types if you want
$FILE_MIMES = array('application/pdf','application/msword');

//Allowable file ext. names. you may add more extension names.
$FILE_EXTS = array('.pdf','.doc');

//Allow file delete? no, if only allow upload only
$DELETABLE = false;


//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// Do not touch the below if you are not confident.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/************************************************************
* Setup variables
************************************************************/
$site_name = "http://www.dkaygraphix.com/zefsci/en/home.php?id=contactopp";
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://www.dkaygraphix.com/zefsci/en/home.php?id=contactopp";

$upload_dir = "files/";
$upload_url = $url_dir."/files/";
$message ="";

/************************************************************
* Create Upload Directory
************************************************************/
if (!is_dir("files")) {
if (!mkdir($upload_dir))
die ("upload_files directory doesn't exist and creation failed");
if (!chmod($upload_dir,0755))
die ("change permission to 755 failed.");
}

/************************************************************
* Process User's Request
************************************************************/
if ($_REQUEST[del] && $DELETABLE) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUEST[del]\n");
fclose($resource);

if (strpos($_REQUEST[del],"/.")>0); //possible hacking
else if (strpos($_REQUEST[del],$upload_dir) === false); //possible hacking
else if (substr($_REQUEST[del],0,6)==$upload_dir) {
unlink($_REQUEST[del]);
print "<script>window.location.href='$url_this&message=deleted successfully'</script>";
}
}
else if ($_FILES['userfile']) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
.$_FILES['userfile']['name']." "
.$_FILES['userfile']['type']."\n");
fclose($resource);

$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));

//File Size Check
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
$message = "The file size is over 75KB.";
//File Type/Extension Check
else if (!in_array($file_type, $FILE_MIMES)
&& !in_array($file_ext, $FILE_EXTS) )
$message = "Sorry, $file_name($file_type) is not allowed to be uploaded.";
else
$message = do_upload($upload_dir, $upload_url);

print "<script>window.location.href='$url_this&message=$message'</script>";
}
else if (!$_FILES['userfile']);
else
$message = "Invalid File Specified.";

/************************************************************
* List Files
************************************************************/
$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist .= "<a href='$upload_dir$file'>".$file."</a>";
if ($DELETABLE)
$filelist .= " <a href='&del=$upload_dir$file' title='delete'>x</a>";
$filelist .= "<sub><small><small><font color=grey> ".date("d-m H:i", filemtime($upload_dir.$file))
."</font></small></small></sub>";
$filelist .="<br>";
}
}

function do_upload($upload_dir, $upload_url) {

$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_path = $upload_dir.$file_name;

//File Name Check
if ( $file_name =="") {
$message = "Invalid File Name Specified";
return $message;
}

$result = move_uploaded_file($temp_name, $file_path);
if (!chmod($file_path,0777))
$message = "change permission to 777 failed.";
else
$message = ($result)?"Thank you for uploading your CV, $file_name has been uploaded successfully." :
"Somthing is wrong with uploading a file.";
return $message;
}
$message = substr($_GET['message'],0, 9);
$message1=$_GET['message'];


Now I can't find the logic as to how to make both of them work together, because after the user clicks on submit to upload the file, it seems like they are redirected to the page $url_this (which is at the top of the code), I can't pass the variables.

I hope that somebody can help me....

Thank you

Yaggles
09-10-2005, 06:18 AM
You just put an if around the code that does the upload that asks if the form was submitted. If it was, you do the upload, and you can check the fields (if's also, using $HTTP_POST_VARS['formfieldname'] as the variable), to make sure that the form fields were used right. If it wasn't submitted, show the form.

grudz
09-10-2005, 11:22 PM
I did, it seems that the variables aren't passed from one page to the next.....

grudz
09-11-2005, 01:18 AM
And here's my code for my form....



if (isset($submit)) {
if (empty($firstname)) {
print '* Please enter your <strong>first name</strong> <br>';
$died = "y";
}
if (empty($lastname)) {
print '* Please enter your <strong>last name</strong> <br>';
$died = "y";
}
if ($country == " ") {
print '* Please select a <strong>country</strong> <br>';
$died = 'y';
}
if (($city_state == "Select Province/State") || ($city_state == " ")) {
print '* Please select a <strong>province or a state</strong> <br>';
$died = 'y';
if (empty($zip_postal)) {
print '* Please enter a <strong>zip/postal code</strong> <br>';
$died = 'y';
}
}
if (empty($email)) {
print '* Please enter your <strong>email address</strong> <br>';
$died = 'y';
}
if (($check_service != "yes") && ($check_qual != "yes") && ($check_app != "yes") && (empty($comments))) {
print "* Please make sure you fill out either the <strong>service</strong>, <strong>qualification</strong>, <strong>application</strong> or <strong>comments</strong> section";
}
if ($died == 'y') {
print "<br><br>";
print "<form name=\"contactform\" method=\"post\" action=\"\">
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr align=\"left\" valign=\"top\">
<td width=\"22%\" height=\"25\" class=\"11BlackNoBold\">First name *</td>
<td width=\"78%\" height=\"25\"><input name=\"firstname\" type=\"text\" class=\"txtBoxContact\" id=\"firstname\" value=".$firstname."></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td width=\"22%\" height=\"25\" class=\"11BlackNoBold\">Last name *</td>
<td width=\"78%\" height=\"25\"><input name=\"lastname\" type=\"text\" class=\"txtBoxContact\" id=\"lastname\" value=".$lastname."></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Organization</td>
<td height=\"25\"><input name=\"organization\" type=\"text\" class=\"txtBoxContact\" id=\"organization\" value=".$organization."></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Title</td>
<td height=\"25\"><input name=\"title\" type=\"text\" class=\"txtBoxContact\" id=\"title\" value=".$title."></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Address</td>
<td height=\"25\"><input name=\"address1\" type=\"text\" class=\"txtBoxContact\" id=\"address1\" style=\"width:255px\" value=".$address1.">
<br>
<input name=\"address2\" type=\"text\" class=\"txtBoxContact\" id=\"address2\" style=\"width:255px\" value=".$address2."></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Country *</td>
<td height=\"25\"><select name=\"country\" size=\"1\" class=\"txtBoxContact\" onchange=\"set_city_state(this,city_state)\" style=\"width:125px\">
<option value=\" \" selected></option>
<option value=\"Canada\">Canada</option>
<option value=\"United Sates\">United States</option></select>
</td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Province/State *</td>
<td height=\"25\"><select name=\"city_state\" size=\"1\" disabled=\"disabled\" class=\"txtBoxContact\">
</select></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Zip/Postal Code *</td>
<td height=\"25\"><input name=\"zip_postal\" type=\"text\" class=\"txtBoxContact\" id=\"zip_postal\" style=\"width:125px\" value=".$zip_postal."></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Telephone</td>
<td height=\"25\">( <input name=\"area1\" type=\"text\" class=\"txtBoxContact\" id=\"area1\" maxlength=\"3\" style=\"width:50px\" value=".$area1."> )
<input name=\"telephone\" type=\"text\" class=\"txtBoxContact\" id=\"telephone\"></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Fax</td>
<td height=\"25\">( <input name=\"area2\" type=\"text\" class=\"txtBoxContact\" maxlength=\"3\" id=\"area2\" style=\"width:50px\" value=".$area2."> )
<input name=\"fax\" type=\"text\" class=\"txtBoxContact\" id=\"fax\">
</td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Email *</td>
<td height=\"25\"><input name=\"email\" type=\"text\" class=\"txtBoxContact\" id=\"email\" value=".$email."></td>
</tr>
<tr align=\"center\" valign=\"middle\">
<td height=\"35\" class=\"12Blue\" colspan=\"2\">* Please fill out at least one of the sections below</td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Service</td>
<td height=\"25\" class=\"11BlackNoBold\"><input name=\"check_service\" type=\"checkbox\" id=\"check_service\" value=\"yes\" value=".$check_service.">
if checked, specify <input name=\"specify_service\" type=\"text\" class=\"txtBoxContact\" id=\"specify_service\" value=".$specify_service."></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Qualification</td>
<td height=\"25\" class=\"11BlackNoBold\"><input name=\"check_qual\" type=\"checkbox\" id=\"check_qual\" value=\"yes\" value=".$check_qual.">
if checked, specify <input name=\"specify_qual\" type=\"text\" class=\"txtBoxContact\" id=\"specify_qual\" value=".$specify_qual."></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Application</td>
<td height=\"25\" class=\"11BlackNoBold\"><input name=\"check_app\" type=\"checkbox\" id=\"check_app\" value=\"yes\" value=".$check_app.">
if checked, specify <input name=\"specify_app\" type=\"text\" class=\"txtBoxContact\" id=\"specify_app\" value=".$check_app."></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td class=\"11BlackNoBold\">Comments</td>
<td><textarea name=\"comments\" class=\"txtBoxContact\" id=\"comments\" style=\"height:60px; width:321px\">".$comments."</textarea></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td class=\"11BlackNoBold\">&nbsp;</td>
<td height=\"25\" valign=\"bottom\"><input type=\"submit\" class=\"button\" value=\"Submit\" name=\"Submit\" id=\"Submit\"></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td>&nbsp;</td>
<td class=\"11BlackNoBold\" height=\"25\" valign=\"bottom\">(* required fields)</td>
</tr>
</table>
</form>";
}
if ($died != "y") {
mail($toaddress, $subject, $mailcontent, $fromheaders);
print "Thank you for submitting your ideas, we will respond to your query as soon as possible";
}
}else{
print "<form name=\"contactform\" method=\"post\" action=\"\">
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr align=\"left\" valign=\"top\">
<td width=\"22%\" height=\"25\" class=\"11BlackNoBold\">First name *</td>
<td width=\"78%\" height=\"25\"><input name=\"firstname\" type=\"text\" class=\"txtBoxContact\" id=\"firstname\"></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td width=\"22%\" height=\"25\" class=\"11BlackNoBold\">Last name *</td>
<td width=\"78%\" height=\"25\"><input name=\"lastname\" type=\"text\" class=\"txtBoxContact\" id=\"lastname\"></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Organization</td>
<td height=\"25\"><input name=\"organization\" type=\"text\" class=\"txtBoxContact\" id=\"organization\"></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Title</td>
<td height=\"25\"><input name=\"title\" type=\"text\" class=\"txtBoxContact\" id=\"title\"></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Address</td>
<td height=\"25\"><input name=\"address1\" type=\"text\" class=\"txtBoxContact\" id=\"address1\" style=\"width:255px\">
<br>
<input name=\"address2\" type=\"text\" class=\"txtBoxContact\" id=\"address2\" style=\"width:255px\"></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Country *</td>
<td height=\"25\"><select name=\"country\" size=\"1\" class=\"txtBoxContact\" onchange=\"set_city_state(this,city_state)\" style=\"width:125px\">
<option value=\" \" selected></option>
<option value=\"Canada\">Canada</option>
<option value=\"United Sates\">United States</option></select>
</td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Province/State *</td>
<td height=\"25\"><select name=\"city_state\" size=\"1\" disabled=\"disabled\" class=\"txtBoxContact\">
</select></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Zip/Postal Code *</td>
<td height=\"25\"><input name=\"zip_postal\" type=\"text\" class=\"txtBoxContact\" id=\"zip_postal\" style=\"width:125px\"></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Telephone</td>
<td height=\"25\">( <input name=\"area1\" type=\"text\" class=\"txtBoxContact\" id=\"area1\" maxlength=\"3\" style=\"width:50px\"> )
<input name=\"telephone\" type=\"text\" class=\"txtBoxContact\" id=\"telephone\"></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Fax</td>
<td height=\"25\">( <input name=\"area2\" type=\"text\" class=\"txtBoxContact\" maxlength=\"3\" id=\"area2\" style=\"width:50px\"> )
<input name=\"fax\" type=\"text\" class=\"txtBoxContact\" id=\"fax\">
</td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Email *</td>
<td height=\"25\"><input name=\"email\" type=\"text\" class=\"txtBoxContact\" id=\"email\"></td>
</tr>
<tr align=\"center\" valign=\"middle\">
<td height=\"35\" class=\"12Blue\" colspan=\"2\">* Please fill out at least one of the sections below</td>
</tr><tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Service</td>
<td height=\"25\" class=\"11BlackNoBold\"><input name=\"check_service\" type=\"checkbox\" id=\"check_service\" value=\"yes\">
if checked, specify <input name=\"specify_service\" type=\"text\" class=\"txtBoxContact\" id=\"email\"></td>
</tr>

<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Qualification</td>
<td height=\"25\" class=\"11BlackNoBold\"><input name=\"check_qual\" type=\"checkbox\" id=\"check_qual\" value=\"yes\">
if checked, specify <input name=\"specify_qual\" type=\"text\" class=\"txtBoxContact\" id=\"specify_qual\"></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td height=\"25\" class=\"11BlackNoBold\">Application</td>
<td height=\"25\" class=\"11BlackNoBold\"><input name=\"check_app\" type=\"checkbox\" id=\"check_app\" value=\"yes\">
if checked, specify <input name=\"specify_app\" type=\"text\" class=\"txtBoxContact\" id=\"specify_app\"></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td class=\"11BlackNoBold\">Comments</td>
<td><textarea name=\"comments\" class=\"txtBoxContact\" id=\"comments\" style=\"height:60px; width:321px\"></textarea></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td class=\"11BlackNoBold\">&nbsp;</td>
<td height=\"25\" valign=\"bottom\"><input type=\"submit\" class=\"button\" value=\"Submit\" name=\"Submit\" id=\"Submit\"></td>
</tr>
<tr align=\"left\" valign=\"top\">
<td>&nbsp;</td>
<td class=\"11BlackNoBold\" height=\"25\" valign=\"bottom\">(* required fields)</td>
</tr>
</table>
</form>";
}

Yaggles
09-11-2005, 02:30 PM
Did you already define those variables with the value of their $HTTP_POST_VARS "twin"? In other words, before doing all those if's, did you say (for instance) $submit = $HTTP_POST_VARS['submit']; ? If not, you have to do either that or just use $HTTP_POST_VARS['variable'] instead of $variable. It does make a difference! ;)

Or am I just missing something really obvious? :P

grudz
09-12-2005, 07:31 PM
I think it's not passing because of this



site_name = "http://www.dkaygraphix.com/zefsci/en/home.php?id=contactopp";
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://www.dkaygraphix.com/zefsci/en/home.php?id=contactopp";


it's sort a redirect, so the variables arent passing.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum