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 have a form for a news portal where the user can upload articles, either typed out text or uplad a pdf file. They can do either, but no both. Here's the code I'm using:

Code: Select all

if ($file=="none" && isset($articleText))
		 					{ 
							    addArticle($articleName,$articleDescr,$articleText,$caoID);
                           		echo "<p align="center"></p><font face="Arial, Helvetica, sans-serif" size="2">Your article has been successfully uploaded. Thank you!</font></p>"; 
         					&#125;
		 					elseif ($file=="none")
							&#123;
								echo "<p align="center"></p><font face="Arial, Helvetica, sans-serif" size="2">You must specify text or a file to upload</p>"; 
							&#125;
							elseif (isset($file))
		 					&#123; 
         						$formats = array('pdf','PDF'); 
            					if(!in_array(substr($file_name,-3),$formats)) 
								&#123; 
            						echo "<font face="verdana,arial,helvetica" size="2" color="#000000"><b>Sorry! Only PDF files are allowed!</b></font>"; 
            					&#125;
								else
								&#123; 
            						copy($file, "../pdf/$file_name"); 
            						unlink($file);
									$articleText = $file_name;
									addArticle($articleName,$articleDescr,$articleText,$caoID);
            						echo "<font face="verdana,arial,helvetica" size="2" color="#000000">File <b>$articleText</b> Uploaded Successfully to directory!</font>"; 
            					&#125;
$artcielText is the name of the text field for typed in data. the addArticle() fuction simpy parses the text and dumps the info into the db. I've set it up so that if a file is uploaded, the article text becomes the file name. When the data is displayed, i do a substr($artcleText, -4) function on the $articleText variable. If the substr() returns a ".pdf" or ".PDF", then the file name is displayed instead of the article text:

Code: Select all

$y = substr($articleText,-4); 
						  if ($y == ".pdf")
						  &#123;
						  	echo "<font face="Arial, Helvetica, sans-serif" size="2"><a href="pdf/$articleText">$articleText</a></font>";
						  &#125;
						  else
						  &#123;
						  	echo $articleText;
						  &#125;
For some reason, everytime I try to just upload text, the page displays the "Sorry, only .pdf files are allowed" error. I can't figure out why this is, maybe I have my if statements in the wrong order?

I appreciate any help on this matter.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

add

Code: Select all

print(htmlentities($file).'<br/>'.htmlentities($articleText));
as print-debugger
Post Reply