Page 1 of 1

Uploading files

Posted: Mon May 27, 2002 3:55 pm
by timmy
I'm trying to create a form for a news portal where a user can either upload text into a database, or upload a PDF file to the server into a directory. They can only do one or the other. Here's what I have:

Code: Select all

if (!isset($file))
								{
									addAnnouncement($announcementName,$announcementDescr,$announcementText,$caoID);
									echo "<p align="center"></p><font face="Arial, Helvetica, sans-serif" size="2">Your announcement has been successfully uploaded. Thank you!</font></p>";
								&#125;
								elseif (isset($file))
								&#123;
									$formats = array('pdf','PDF');
									if(!in_array(strtolower(substr($file_name,-3)),$formats)) 
									&#123;
									echo "<font face="arial,helvetica, sans serif" size="2" color="#000000"><b>There was an error uploading your file! <br>Please make sure you are only uploading only pdf files! Other file types will be rejected.</b></font>"; 
									&#125;
									else
									&#123;
									copy($file, "../pdf/$file_name"); 
									unlink($file);
									$announcementText = $file_name;
									addAnnouncement($announcementName,$announcementDescr,$announcementText,$caoID);
									echo "<p align="center"></p><font face="Arial, Helvetica, sans-serif" size="2">Your announcement has been successfully uploaded. Thank you!</font></p>";
									&#125;
								&#125;
The addAnnouncement() is just a simple function to addslashes to the text and upload the text from the form into a database using an INSERT statement.

For some reason, the script is compleyely ignoring my if{} statement, and no matter what I do, either try to type in text in the text field or try to upload a file, the script always ends up du ping the error text: "There was an error uploading your file! <br>Please make sure you are only uploading only pdf files! Other file types will be rejected." I've used pretty much this same script on other wesbites, but haven't had this many problems. I only want user to be able to upload >pdf files, nothing else, hence the if(!in_array(...) statement. If the file extension is not ".pdf" or ".PDF", then the file gets rejected and nothing happens.

I was hoping someone could help me here, maybe suggest a better way of doing this or pointing me in the proper direction as to where I may find a better example to edit to my own needs.

thanks

Posted: Mon May 27, 2002 4:52 pm
by hob_goblin
i dont know, it looks like it all checks out, i can't find any errors

one thing i did notice was that in your $filetype array i saw pdf and PDF

why would you need PDF if you used strtolower()

Posted: Mon May 27, 2002 5:51 pm
by Benjamin
I think the problem may be your brackets. Try this

Code: Select all

if (!isset($file)) 
                        &#123; 
                           addAnnouncement($announcementName,$announcementDescr,$announcementText,$caoID); 
                           echo "<p align="center"></p><font face="Arial, Helvetica, sans-serif" size="2">Your announcement has been successfully uploaded. Thank you!</font></p>"; 
                        &#125; 

elseif (isset($file)) 
                        &#123; 
                           $formats = array('pdf','PDF'); 
                           if(!in_array(strtolower(substr($file_name,-3)),$formats)) 
                        	 &#123; 
                           echo "<font face="arial,helvetica, sans serif" size="2" color="#000000"><b>There was an error uploading your file! <br>Please make sure you are only uploading only pdf files! Other file types will be rejected.</b></font>"; 
                           	&#125; 
		&#125;
else 
                           &#123; 
                           copy($file, "../pdf/$file_name"); 
                           unlink($file); 
                           $announcementText = $file_name; 
                           addAnnouncement($announcementName,$announcementDescr,$announcementText,$caoID); 
                           echo "<p align="center"></p><font face="Arial, Helvetica, sans-serif" size="2">Your announcement has been successfully uploaded. Thank you!</font></p>"; 
                           &#125;