Uploading Files
Posted: Sat Jun 22, 2002 1:55 pm
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:
$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:
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.
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>";
}
elseif ($file=="none")
{
echo "<p align="center"></p><font face="Arial, Helvetica, sans-serif" size="2">You must specify text or a file to upload</p>";
}
elseif (isset($file))
{
$formats = array('pdf','PDF');
if(!in_array(substr($file_name,-3),$formats))
{
echo "<font face="verdana,arial,helvetica" size="2" color="#000000"><b>Sorry! Only PDF files are allowed!</b></font>";
}
else
{
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>";
}Code: Select all
$y = substr($articleText,-4);
if ($y == ".pdf")
{
echo "<font face="Arial, Helvetica, sans-serif" size="2"><a href="pdf/$articleText">$articleText</a></font>";
}
else
{
echo $articleText;
}I appreciate any help on this matter.