This script works about 99% of the time, but the other 1% of the time I get notice undefined index uploaded_file every place I use the $_FILES array.
the full reference to the file is $_FILES['uploaded_file']['whatever'][$index]
where index is 0,1,or2 (we can receive 3 files)
in the html form I use name=uploaded_file[]
this problem has happened twice now that I know of.
once was when one of our employees tried to send a file to another store and once was with a customer.
It has happened on both a Windows box and on an OS9 box.
The OS9 box did it for about 15 minutes then started working again.
I use php 4.3.0
My question is, is this a problem in my php or in the web browser?
also is my use of file_name[] possibly problematic?
I am posting this here instead of figuring it out because I cannot force the problem to debug it on my own.
the form is at:
http://www.azteccopies.com
then click on submit jobs online (please do not send us lots of junk and in the email address form make it ignore@emailname@domain.com (this way it can be quickly weeded out of our work flow, but I can contact you))
You can also just use ignore@ignore or something if you do not want to be contacted.
here is the script I use (warning poor code my cause eyes to bleed).
The html stuff is to insert a table into a results page, but my only problem is with the places I use $_FILES[] the lines that loop come up 3 times (obviously).
The search and replace is because cooky mac designers were sending files like
*something_12/4/2002 rev4.3
and our unix server was not happy.
I think everything else has comments.
Code: Select all
//ob_end_flush(); /*uncomment for degugging*/
$file_nameї0] = "";
$file_nameї1] = "";
$file_nameї2] = "";
if ($_POSTї'recipient']=="censored@azteccopies.com") {$store="trolley";
} elseif (
$_POSTї'recipient']=="censored@azteccopies.com") {$store="talley";
} elseif (
$_POSTї'recipient']=="censored@azteccopies.com") {$store="pike";
} else {
$_POSTї'recipient']="censored@azteccopies.com"; $store="";}
/* the block above this verifies a valid destination email (unfortunatly hard coded, move to editable array in future)
This way we cannot be abused as an anonymous relay */
$error_num_stat = 0;
/*This line sets the error tracker to zero, it the error codes are set up as binary flags, but not yet used as such*/
/* the next three blocks of code are the file upload, they verify the field was filled (is_uploades_file) then move the file, if there is an error they flag either 001 010 100 bi
nary (1 2 4). the upload directory is a predefined prefix with the store name attached. */
$limit = array(0,1,2);
//$index=0;
foreach(array(0,1,2) as $index) {
if ($_FILESї'uploaded_file']ї'name']ї$index] != "") {
$file_nameї$index] = $_FILESї'uploaded_file']ї'name']ї$index];
$file_nameї$index] = str_replace(array('*', '?', '!' ,'|' ,'\'' ,'/' ,'''', '"' ,'{', '}', '<' ,'>', ';', ',', '^', '(', ')', '$', '~'),
'_', $file_nameї$index]);
if (/* if upload fails (not succeeds)*/
!move_uploaded_file($_FILESї'uploaded_file']ї'tmp_name']ї$index],
"/censored/file_nameї$index]))
{$error_num_stat = pow(2,$index);}/*set the appropriate the flags are not used*/
}
}
/*if upload was good, send us an email*/
if ($error_num_stat==0){
mail($_POSTї'recipient'], "Customer File Upload", $_POSTї'Name']." sent the following file(s)
\nfile 1:". $file_nameї0]."
\nfile 2:". $file_nameї1]."
\nfile 3:". $file_nameї2]."
\nThey should be available for you at the following website:
\nhttp://www.azteccopies.com/censored/
\nplatform: ". $_POSTї'Platform'] ."
\nphone: ".$_POSTї'Phone']."
\nfax: ".$_POSTї'fax']."
\ndue: ".$_POSTї'due_by']."
\nemail: ".$_POSTї'email']."
\nInstructions: ".$_POSTї'instructions'],"From: ".$_POSTї'email']."\n");
//header ("location:thanks.html"); /*obsolete line if I fix this*/
$FILE1_string = $_FILESї'uploaded_file']ї'name']ї0];
$FILE2_string = $_FILESї'uploaded_file']ї'name']ї1];
$FILE3_string = $_FILESї'uploaded_file']ї'name']ї2];
//$FILES_size = array($_FILESї'uploaded_file']ї'size']ї0],$_FILESї'uploaded_file']ї'size']ї1],$_FILESї'uploaded_file']ї'size']ї2]);
foreach(array(0,1,2) as $index) {
$FILES_sizeї$index] = size_convert($_FILESї'uploaded_file']ї'size']ї$index]);
}
$files_string = "<br><br>We recived the following files from you:<br><br>
<table cellpadding="2" cellspacing="2" border="0" color="#cc9900"
style="text-align: center; width: 300px; font-size: 85%; font-family:Verdana, Arial, Helvetica, sans-serif; font-weight: bold;">
<tbody>
<tr>
<td style="text-align: center;">File Name<br>
</td>
<td>Size<br>
</td>
</tr>
<tr>
<td>".$_FILESї'uploaded_file']ї'name']ї0]."<br></td>
<td>".$FILES_sizeї0]."</td>
</tr>
<tr>
<td>".$_FILESї'uploaded_file']ї'name']ї1]."<br></td>
<td>".$FILES_sizeї1]."</td>
</tr>
<tr>
<td>".$_FILESї'uploaded_file']ї'name']ї2]."<br></td>
<td>".$FILES_sizeї2]."</td>
</tr>
</tbody>
</table>";
//<br>$FILE1_string <br> $FILE2_string <br> $FILE3_string <br><br>";
// use color #C26100 for file names.
$thanksstring = file_get_contents("./thanks.html");
$thanksstring = str_replace('<!--TEXTHERE-->',"$files_string",$thanksstring);
print("$thanksstring"); /*otherwise print a bland error screen (should not happen ever)*/
}else{
print("<h1>There was a problem when uploading your file(s)</h1> <p>Please retry the file upload</p> <p>If this is a repeat error then call the location closest to you</p>
<p>error code:$error_num_stat");
}
function size_convert($file_size){
if($file_size >= 1073741824)
{
$file_size = round($file_size / 1073741824 * 100) / 100 . "GB";
}
elseif($file_size >= 1048576)
{
$file_size = round($file_size / 1048575 * 100) / 100 . "MB";
}
elseif($file_size >= 1024)
: {
$file_size = round($file_size / 1024 * 100) / 100 . "KB";
}
elseif($file_size > 0)
{
$file_size = $file_size . "bytes";
}
else
{
$file_size = "";
}
return($file_size);
}