Log in

View Full Version : Help me Validate this



jmituzas
08-14-2010, 02:48 AM
Ok I have a php upload form for multiple files. Works great.

I need it to only accept certain file types. These file types include .pdf .ai and .doc. Is there a way to get this to work with form validation?
And can we validate against file name (that would be great!)
Please help me out.
Thanks in advance,
Regards.

heres the working form:

my index.html file:

<table class="sample" align="center" width="725">
<tr><form action="multiple_upload_ac.php" method="post" enctype="multipart/form-data" name="form1" id="form1"><td>
<table align="left" width="620">
<tr><br>&nbsp;<br><center><font color="#015697"><b>Upload Requested Files</b><br>&nbsp;</tr>
<tr><td><font color="#015697"><b>Select Executed Contract.pdf<input name="ufile[]" type="file" id="ufile[]" size="50" /></b><br>&nbsp;</td></tr>
<tr><td><font color="#015697"><b>Select Marketing Assessment Form.doc<input name="ufile[]" type="file" id="ufile[]" size="50" /></b><br>&nbsp;</td></tr>
<tr><td><font color="#015697"><b>Select W-9.pdf<input name="ufile[]" type="file" id="ufile[]" size="50" /></b><br>&nbsp;</td></tr>
<tr><td><font color="#015697"><b>Select Logo.ai<input name="ufile[]" type="file" id="ufile[]" size="50" /></b><br>&nbsp;</td></tr>
<tr><td align="left"><input type="submit" name="Submit" value="Upload" /></td></tr>
<tr></tr>
</table>
</td>
</form>
</tr>
</table>

my multiple_upload_ac.php file:

<?php
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
$path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];
$path4= "upload/".$HTTP_POST_FILES['ufile']['name'][3];


//copy file to where you want to store file
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);
copy($HTTP_POST_FILES['ufile']['tmp_name'][3], $path4);

//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>";
echo "<img src=\"$path1\" width=\"250\" height=\"150\">";
echo "<P>";

echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>";
echo "<img src=\"$path2\" width=\"250\" height=\"150\">";
echo "<P>";

echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>";
echo "<img src=\"$path3\" width=\"250\" height=\"150\">";

echo "File Name :".$HTTP_POST_FILES['ufile']['name'][3]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][3]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][3]."<BR/>";
echo "<img src=\"$path3\" width=\"250\" height=\"150\">";

///////////////////////////////////////////////////////

// Use this code to display the error or success.

$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][1];
$filesize3=$HTTP_POST_FILES['ufile']['size'][2];
$filesize4=$HTTP_POST_FILES['ufile']['size'][3];


if($filesize1 && $filesize2 && $filesize3 && $filesize4 != 0)
{
echo "We have recieved your files";
}

else {
echo "ERROR.....";
}

//////////////////////////////////////////////

// What files that have a problem? (if found)

if($filesize1==0) {
echo "There're something error in your first file";
echo "<BR />";
}

if($filesize2==0) {
echo "There're something error in your second file";
echo "<BR />";
}

if($filesize3==0) {
echo "There're something error in your third file";
echo "<BR />";
}

if($filesize4==0) {
echo "There're something error in your forth file";
echo "<BR />";
}

?>

Again Thanks in advance!