Jump to content

[SOLVED] function checktype is not working?


sayedsohail

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
Share on other sites

hi, not sure why do you use loop while..... ?

are you uploading more files than one at a time ?

 

because you can use simple condition

 

if($_FILES['file']['type'] == "application/pdf") {
return true;
}else{
return false;
}

 

hope it helps

Link to comment
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";
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";
}


?>

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

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.