Jump to content

Recommended Posts

I am having a problem with this function, i don't know why this is just returning false all the time, even though when there is a file sent through the post.

 

function checkType() {	
while(list($key,$value) = each($_FILES[images][type])){
	strtolower($value);
	if(!empty($value))
	{
		if($value != "application/pdf")
		{return false;}
		else { return true;}

	}

}

}

if (checkType()==FALSE)
{
$errors[]="Sorry , current format is <b>'.($value).'</b> ,only html or pdf files are allowed.";
}

Link to comment
https://forums.phpfreaks.com/topic/66627-solved-function-checktype-is-not-working/
Share on other sites

I added a form, which i assume your using..

 

this should work fine

<form method="post" enctype="multipart/form-data" >
<input name="images[]" type="file" /><br />
<input name="images[]" type="file" /><br />
<input name="images[]" type="file" /><br />
<input name="submit" type="submit" value="sumit" />
</form>
<?php
function checkType()
{
foreach($_FILES['images']['type'] as $theType)
{
	$value = strtolower($theType);
	if(!empty($value))
	{
		if($value != "application/pdf")
		{
			return false;
		}else{
			return true;
		}
	}
}
}

if (checkType()===FALSE)
{
$errors[]="Sorry , current format is <b>'.($value).'</b> ,only html or pdf files are allowed.";
}else{
echo "WooHoo";
}

if no files are uploaded it will fail

 

function checkType()
{
foreach($_FILES['images']['type'] as $theType)
{
	$value = strtolower($theType);
	if(!empty($value))
	{
		if($value != "application/pdf")
		{
			return false;
		}
	}
}
return true;//or false if they must upload
}

 

Updated

thanks madtechie,

 

It worked for me in IE, but for some strange reason the latest firefox is giving this error.

 

Sorry , current format is (application/octet-stream) ,only html or pdf files are allowed?

 

Does it has to do something WITH pdf filenames, i.e., my file name is 2007-08-08CAT-Q12345ABCDEFGHIJK10.pdf

 

 

<?$errors = array();
?>
<form method="post" enctype="multipart/form-data" >
<input name="images[]" type="file" /><br />
<input name="images[]" type="file" /><br />
<input name="images[]" type="file" /><br />
<input name="submit" type="submit" value="sumit" />
</form>
<?php
function checkType()
{
foreach($_FILES['images']['type'] as $theType)
{
	$value = strtolower($theType);
	if(!empty($value))
	{
		if($value != "application/pdf")
		{
			return false;
		}else{
			return true;
		}
	}
}
}

if (checkType()===FALSE)
{
$errors[]="Sorry , current format is <b>($value)</b> ,only pdf files are allowed.";
}else{
echo "WooHoo";
}

that code should work fine for both IE & FF,

 

 

Use this code in its own file and let me know if you get the same error.

 

if no files are uploaded it will fail

 

function checkType()
{
foreach($_FILES['images']['type'] as $theType)
{
	$value = strtolower($theType);
	if(!empty($value))
	{
		if($value != "application/pdf")
		{
			return false;
		}
	}
}
return true;//or false if they must upload
}

 

Updated

i just tested this is FF 2.0.0.6, no errors

 

<form method="post" enctype="multipart/form-data" >
<input name="images[]" type="file" /><br />
<input name="images[]" type="file" /><br />
<input name="images[]" type="file" /><br />
<input name="submit" type="submit" value="sumit" />
</form>
<?php
function checkType()
{
if( isset($_FILES) ) return true;
foreach($_FILES['images']['type'] as $theType)
{
	$value = strtolower($theType);
	if(!empty($value))
	{
		if($value != "application/pdf")
		{
			return false;
		}
	}
}
return true;
}

if (checkType()===FALSE)
{
$errors[]="Sorry , current format is <b>'.($value).'</b> ,only html or pdf files are allowed.";
}else{
echo "WooHoo";
}


?>

it seems its working on your site, here is the code i used iframe to display errors.

 

form.php

<?
session_start();

?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<?php
echo "<html><head><title>Email Form</title></head>";
$max_no_img=4;  // Maximum number of images value to be set here
echo "<form method='post' action='confirmation.php' enctype='multipart/form-data' target='errorframe'>";
echo "<table class='test' width='100%'>";
echo "<tr height='25px' align='center'><th class ='overState' colspan='4'><font face='verdana' size='2' color='navy' >Email pdf</font></th></tr>
</table>

<table class='test'>
<tr><td>To: </td><td><input type='text' name='to' value='' /></td>
<td>CC: </td><td><input type='text' name='cc' value='' /></td></tr>

<tr><td>BC: </td><td><input type='text' name='bc' value='' /></td>

<td>From: </td><td><input type='text' name='from' value='XYZ@googlemail.com' /></td></tr>

<tr><td>Subject: </td><td colspan='3'><input type='text' name='subject' value='pdf Attached' /></td></tr>
<tr><td>Message:</td><td colspan='3'> </td></tr>
<tr><td> </td><td colspan='3'><textarea cols='60' rows='7' name='message' value=''></textarea></td></tr>";

for($i=1; $i<=$max_no_img; $i++){
echo "<tr><td>Files $i</td colspan='3'><td>
<input type='file' id='pdfs[]' name='pdfs[]' value='' class='formbutton'></td></tr>";
}
echo "<br><TR>
<td align='right' colspan='2'>
<input class='formbutton' type='submit' name ='submit' value='Send' ></td>
<td align='left' align='left' colspan='2'><input class='formbutton' name='exit' type='button' id='exit' onclick='self.close();' value='Close' ></td>
</tr>";

echo "</table></form>";
<table width='100%'><tr><Td><iframe id='errorframe' name='errorframe' style='border:0px;'></iframe></td></tr></table>
</body>

</html>";
?>

 

Here is the confirmation.php

function checkType()
{

foreach($_FILES['pdfs']['type'] as $theType)
{
	$value = strtolower($theType);
	if(!empty($value))
	{
		if($value != "application/pdf")
		{
			return false;
		}
	}
}
return true;//or false if they must upload
}

if (checkType()==FALSE)
{
$errors[]="Sorry , current format is <b>($value)</b> ,only pdf files are allowed.";
}else{
$errors[]="WooHoo format is OK";
} 

....foreach($errors as $err).... to display errors.

 

 

change

for($i=1; $i<=$max_no_img; $i++){
echo "<tr><td>Files $i</td colspan='3'><td>
<input type='file' id='pdfs[]' name='pdfs[]' value='' class='formbutton'></td></tr>";
}

 

to

 

for($i=1; $i<=$max_no_img; $i++){
echo "<tr><td>Files $i</td colspan='3'><td>
<input type='file' name='pdfs[]' value='' class='formbutton'></td></tr>";
}

 

it should be fine

Changed but still the the same error, even after downloading a pdf (www.irs.gov/pub/irs-pdf/fw4.pdf) for test purpose from internet.  Also updated the firefox just know.

 

this is what it shows on ff Sorry , current format is (application/octet-stream) ,only pdf files are allowed but in IE it works fine.

 

 

Nope still no problem here

 

http://madtechiesupport.com/TestForm.php

 

 

 

<?
session_start();

function checkType()
{
	if( isset($_FILES) ) return true;
	foreach($_FILES['images']['type'] as $theType)
	{
		$value = strtolower($theType);
		if(!empty($value))
		{
			if($value != "application/pdf")
			{
				return false;
			}
		}
	}
	return true;
}

if(isset($_POST['submit']))
{
	if (checkType()===FALSE)
	{
		$errors[]="Sorry , current format is <b>'.($value).'</b> ,only html or pdf files are allowed.";
		echo "<pre>";
		print_r($errors);
	}else{
		echo "WooHoo";
	}
	die;
}


?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<?php
echo "<html><head><title>Email Form</title></head>";
$max_no_img=4;  // Maximum number of images value to be set here
echo "<form method='post' enctype='multipart/form-data' target='errorframe'>";
echo "<table class='test' width='100%'>";
echo "<tr height='25px' align='center'><th class ='overState' colspan='4'><font face='verdana' size='2' color='navy' >Email pdf</font></th></tr>
</table>

<table class='test'>
<tr><td>To: </td><td><input type='text' name='to' value='' /></td>
<td>CC: </td><td><input type='text' name='cc' value='' /></td></tr>

<tr><td>BC: </td><td><input type='text' name='bc' value='' /></td>

<td>From: </td><td><input type='text' name='from' value='XYZ@googlemail.com' /></td></tr>

<tr><td>Subject: </td><td colspan='3'><input type='text' name='subject' value='pdf Attached' /></td></tr>
<tr><td>Message:</td><td colspan='3'> </td></tr>
<tr><td> </td><td colspan='3'><textarea cols='60' rows='7' name='message' value=''></textarea></td></tr>";

for($i=1; $i<=$max_no_img; $i++){
echo "<tr><td>Files $i</td colspan='3'><td>



<input type='file' id='pdfs[]' name='pdfs[]' value='' class='formbutton'></td></tr>";
}
echo "<br><TR>
<td align='right' colspan='2'>
<input class='formbutton' type='submit' name ='submit' value='Send' ></td>
<td align='left' align='left' colspan='2'><input class='formbutton' name='exit' type='button' id='exit' onclick='self.close();' value='Close' ></td>
</tr>";

echo "</table></form>";
echo "<table width='100%'><tr><Td><iframe id='errorframe' name='errorframe' style='border:0px;'></iframe></td></tr></table>
</body>

</html>";
?>

Million thanks madtechie, but i can't cope this anymore, its really making me mad and it 3:20am over here, i am just giving it up now until this afternoon.

 

But million thanks for your help. I was able to fix the function routines, which is working fine in IE.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.