I can upload the image file to the file directory with no problem, but the file name is not getting posted to the file page. Also, I'm not using a dbase, just posting the content to a new file called style.css
Here is the code I have been testing in the form for the file upload.
There is one thing I was wondering about though. Does anyone know how to setup the image upload so that an user can select at the same time an image from the drop down menu or browse to select an image from their computer. Currently, since the menu select posting is like this " . $_POST['field_2'] . " and the file upload posting is like this ".$field_2_filename.", you cannot concurrently use both at the same time to get the needed results.
Regards,
Charles
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello Support,
I can upload the image file to the file directory with no problem, but the file name is not getting posted to the file page. Also, I'm not using a dbase, just posting the content to a new file called style.css
Here is the code I have been testing in the form for the file upload.
<code>
<form method=post enctype=multipart/form-data action=processor.php onSubmit="return validatePage1();"><ul class=mainForm id="mainForm_1">
<li class="mainForm" id="fieldBox_1">
<label class="formFieldQuestion">Field question 1 *</label><input class=mainForm type=file name=field_1 id=field_1 value=""></li>
<li class="mainForm" id="fieldBox_2">
<label class="formFieldQuestion">Field question 2 *</label><input class=mainForm type=file name=field_2 id=field_2 value=""></li>
<li class="mainForm" id="fieldBox_3">
<label class="formFieldQuestion">Field question 3</label><input class=mainForm type=file name=field_3 id=field_3 value=""></li>
<li class="mainForm" id="fieldBox_4">
<label class="formFieldQuestion">Field question 4</label><input class=mainForm type=file name=field_4 id=field_4 value=""></li>
<SCRIPT type=text/javascript>
<!--
function validatePage1()
{
retVal = true;
if (validateField('field_1','fieldBox_1','file',1) == false)
retVal=false;
if (validateField('field_2','fieldBox_2','file',1) == false)
retVal=false;
if (validateField('field_3','fieldBox_3','file',0) == false)
retVal=false;
if (validateField('field_4','fieldBox_4','file',0) == false)
retVal=false;
if(retVal == false)
{
alert('Please correct the errors. Fields marked with an asterisk (*) are required');
return false;
}
return retVal;
}
//-->
</SCRIPT>
</code>
And, here is the code in the processor.php file.
<code>
<?php
$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
session_start();
if( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) ) {
// File upload handling
if($_FILES['field_1']['name']!=''){
$field_1_filename = "file_1_".date("sihdmY").substr($_FILES['field_1']['name'],strlen($_FILES['field_1']['name'])-4);
if(!move_uploaded_file($_FILES['field_1']['tmp_name'], "./files/".$field_1_filename)){
die("File " . $_FILES['field_1']['name'] . " was not uploaded.");
}
}
// File upload handling
if($_FILES['field_2']['name']!=''){
$field_2_filename = "file_2_".date("sihdmY").substr($_FILES['field_2']['name'],strlen($_FILES['field_2']['name'])-4);
if(!move_uploaded_file($_FILES['field_2']['tmp_name'], "./files/".$field_2_filename)){
die("File " . $_FILES['field_2']['name'] . " was not uploaded.");
}
}
// File upload handling
if($_FILES['field_3']['name']!=''){
$field_3_filename = "file_3_".date("sihdmY").substr($_FILES['field_3']['name'],strlen($_FILES['field_3']['name'])-4);
if(!move_uploaded_file($_FILES['field_3']['tmp_name'], "./files/".$field_3_filename)){
die("File " . $_FILES['field_3']['name'] . " was not uploaded.");
}
}
// File upload handling
if($_FILES['field_4']['name']!=''){
$field_4_filename = "file_4_".date("sihdmY").substr($_FILES['field_4']['name'],strlen($_FILES['field_4']['name'])-4);
if(!move_uploaded_file($_FILES['field_4']['tmp_name'], "./files/".$field_4_filename)){
die("File " . $_FILES['field_4']['name'] . " was not uploaded.");
}
}
$fileLine = "Field question 1: " . $_POST['field_1_filename'] . "
Field question 2: " . $_POST['field_2_filename'] . "
Field question 3: " . $_POST['field_3'] . "
Field question 4: " . $_POST['field_4'] . "
";
$filename = 'style.css';
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a'))
{
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $fileLine) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
} else {
echo "The file is not writable";
}
include("confirm.html");
}
else {
echo "Invalid Captcha String.";
}
?>
</code>
And, here is the resulting style.css file when I submit the form.
Field question 1:
Field question 2:
Field question 3:
Field question 4:
As you can see the field question 1-4 are empty. They don't have the file names posted.
Does anyone know how to fix this. Any help would be greatly appreciated.
Thanks
CHILL
This part is an error on my part.
$fileLine = "Field question 1: " . $_POST['field_1_filename'] . "
Field question 2: " . $_POST['field_2_filename'] . "
It should read the following:
$fileLine = "Field question 1: " . $_POST['field_1'] . "
Field question 2: " . $_POST['field_2'] . "
Nevertheless, the problem still persists.
Regards,
CHILL
I figured out the problem. I found the solution in this post: http://sourceforge.net/forum/message.php?msg_id=7457449
Evidently, this little bit of code is the trick. Thank you Andrew Tait!
Image upload: ".$where_form_is."files/".$field_5_filename." (original file name: " . $_FILES['field_5']['name'] . ")
There is one thing I was wondering about though. Does anyone know how to setup the image upload so that an user can select at the same time an image from the drop down menu or browse to select an image from their computer. Currently, since the menu select posting is like this " . $_POST['field_2'] . " and the file upload posting is like this ".$field_2_filename.", you cannot concurrently use both at the same time to get the needed results.
Regards,
Charles