sayedsohail Posted August 25, 2007 Share Posted August 25, 2007 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."; } Quote Link to comment Share on other sites More sharing options...
xpace Posted August 25, 2007 Share Posted August 25, 2007 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 Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted August 25, 2007 Author Share Posted August 25, 2007 yes i am uploading more than one file sometimes, i got four file buttons in my email form incase if user wish to submit more than one file at a time. Quote Link to comment Share on other sites More sharing options...
xpace Posted August 25, 2007 Share Posted August 25, 2007 in that case can you post more of the code, the actual processing of files and/or form for uploading files ? especially part with variables $key and $value Quote Link to comment Share on other sites More sharing options...
MadTechie Posted August 25, 2007 Share Posted August 25, 2007 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"; } Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted August 25, 2007 Author Share Posted August 25, 2007 you are right, its working, but i would like raise an error if no files are empty or just display a information to the user that, the "email is submitted without any attachments". Quote Link to comment Share on other sites More sharing options...
MadTechie Posted August 25, 2007 Share Posted August 25, 2007 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 Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted August 25, 2007 Author Share Posted August 25, 2007 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"; } Quote Link to comment Share on other sites More sharing options...
MadTechie Posted August 25, 2007 Share Posted August 25, 2007 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 Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted August 25, 2007 Author Share Posted August 25, 2007 still the same for FF? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted August 25, 2007 Share Posted August 25, 2007 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"; } ?> Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted August 25, 2007 Author Share Posted August 25, 2007 i will update my ff although its been uploaded a week ago. i got 2.0.0.6 aswell. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted August 26, 2007 Share Posted August 26, 2007 try here http://madtechiesupport.com/TestCheckType.php Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted August 26, 2007 Author Share Posted August 26, 2007 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. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted August 26, 2007 Share Posted August 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted August 26, 2007 Author Share Posted August 26, 2007 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. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted August 26, 2007 Share Posted August 26, 2007 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>"; ?> Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted August 26, 2007 Author Share Posted August 26, 2007 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. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted August 26, 2007 Share Posted August 26, 2007 you from the UK as well (being 3:20am too) Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted August 26, 2007 Author Share Posted August 26, 2007 indeed - the last bit i am trying to do is to install acrobat reader and see if it make any difference. Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted August 26, 2007 Author Share Posted August 26, 2007 For some strange reason, as soon as i installed the acrobat reader, the functions started behaving normal in ff. atleast i don't have to worry for this weekend. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted August 26, 2007 Share Posted August 26, 2007 Humm, if you goto Options -> contents -> filetype (Manage button) i think that controls the mime Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.