Uploading files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
timmy
Forum Newbie
Posts: 15
Joined: Fri Apr 19, 2002 9:45 am
Location: Calgary, AB

Uploading files

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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()
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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;
Post Reply