Page 1 of 1

if(file_exists()) [SOLVED]

Posted: Thu Apr 14, 2005 3:53 pm
by melindaSA
I am alowing users to upload MSword or PDF files, and the files are named company.doc or company.pdf. What I would like to do is be able to show these files using:

Code: Select all

<?php
if(file_exists("../uploads/".$company.".doc")){
echo <a hef=\"../uploads/".$company.".doc\">Yes</a></font><br></td>\n";
} else {

echo "No";
}
?>
But the above only looks for .doc how do I have it look for .doc or .pdf?

Any help would be appreciated!

Posted: Thu Apr 14, 2005 3:59 pm
by shiznatix

Code: Select all

<?php
if(file_exists("../uploads/".$company.".doc")){
echo <a hef="../uploads/".$company.".doc\">Yes</a></font><br></td>\n";
} elseif(file_exists("../uploads/".$company.".pdf")) {
 echo <a hef="../uploads/".$company.".pdf\">Yes</a></font><br></td>\n";
}else{
echo "no";
}
?>

Posted: Thu Apr 14, 2005 4:57 pm
by pickle

Code: Select all

<?PHP
$files = array("../uploads/".$company.".doc",
               "../uploads/".$company.".pdf");
foreach($files as $path)
{
   $document = basename($path);
   echo "<a href = '$path'>Download: $document</a><br />";
}
?>

Posted: Thu Apr 14, 2005 5:11 pm
by melindaSA
Thank you, but that does not work... --I have attached all my code.....in case I have another error!!

Code: Select all

<?php
require_once('positions_inc_fns.php');
session_start();

$conn = db_connect();
if (check_admin_user())
{
$query = "SELECT * FROM tblmember where status = 1";
$result = mysql_query($query) or die ("My SQL Error: <b>".mysql_error());

if (isset($_GET['del']))
{
	$sqldel = 'delete from tblmember where id = '.$_GET['delid'];
	$querydel = mysql_query($sqldel) or die('Cannot query the database4  .<b>' . mysql_error()); 
}
        
echo "<table border=\"0\" width=\"850\" cellpadding=\"0\" cellspacing=\"0\"><tr>";
echo "<td width=\"260\"><font face=\"Verdana\" color=\"white\" size=\"2\"><b>Name</b></font></td>";
echo "<td width=\"220\"><font face=\"Verdana\" color=\"white\" size=\"2\"><b>Company</b></font></td>";
echo "<td width=\"80\"><font face=\"Verdana\" color=\"white\" size=\"2\"><b>File</b></font></td>";
echo "<td width=\"80\"><font face=\"Verdana\" color=\"white\" size=\"2\"><b>Delete</b></font></td>";
echo "<td width=\"80\"><font face=\"Verdana\" color=\"white\" size=\"2\"><b>Send Login</b></font></td>";

echo "</tr>";
echo "<tr><td>&nbsp;</td></tr>";

while ( $row = mysql_fetch_array($result))
{
   extract($row);
   echo "<br><tr>\n";
   echo "<td valign=\"top\" width=\"260\"><font face=\"Verdana\" color=\"white\" size=\"1\">
   
    <a href=\"javascript:void(0);\" onclick=\"window.open('show_members.php?id=$id','PageName','location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=630,height=420')\">
   $fname $lname</a></font><br></td>\n";
   echo "<td valign=\"top\" width=\"220\"><font face=\"Verdana\" color=\"white\" size=\"1\">$company</font><br></td>\n";
   
if(file_exists("../uploads/".$company.".doc")){
echo "<td valign=\"top\" width=\"80\"><font face=\"Verdana\" size=\"1\"><a href=\"upload/\".$company.\".doc\" target=\"_blank\">Yes</a></font><br></td>\n";
} elseif(file_exists("../uploads/".$company.".pdf")) {
echo "<td valign=\"top\" width=\"80\"><font face=\"Verdana\" size=\"1\"><a href=\"upload/\".$company.\".pdf\" target=\"_blank\">Yes</a></font><br></td>\n";
} else {
echo "<td valign=\"top\" width=\"80\"><font face=\"Verdana\" color=\"white\" size=\"1\">No</font><br></td>\n";
}
echo "<td valign=\"top\" width=\"80\"><font face=\"Verdana\" color=\"white\" size=\"1\"><a href=\"delete_members.php?id=$id\" target=\"_blank\">delete</a></font><br></td>\n";
echo "<td valign=\"top\" width=\"80\"><font face=\"Verdana\" color=\"white\" size=\"1\"><a href=\"#\" class=\"lnk\" onClick=\"window.open('sendunamepwd.php?id=$id','','top=50,left=50,width=300,height=270')\"><font color=\"white\">Send </font></td>\n";
}

   echo "<td valign=\"top\" align=\"center\" width=\"80\">";
   echo "</td></tr></table>\n";

}
else
echo 'You are not authorized to enter the administration area.';
echo mysql_errno() . ": " . mysql_error(). "\n";

?>

Posted: Fri Apr 15, 2005 9:31 am
by melindaSA
Thank you Pickle!

It is almost working!!

This is the result:
Name-------------Company----------File---------------------------------
Melinda Cooper MC eConsulting MC eConsulting.doc MC eConsulting.pdf delete Send
It is showing both .pdf and .doc but there is only a pdf, and the links do not work.

Help!
Thank you!

Posted: Fri Apr 15, 2005 9:52 am
by Bennettman
Mod of pickle's code:

Code: Select all

<?PHP
$files = array("../uploads/".$company.".doc",
               "../uploads/".$company.".pdf");
foreach($files as $path)
{
   if (file_exists($path))
   {
      $document = basename($path);
      echo "<a href='$path'>Download: $document</a><br />";
   }
}
?>

Posted: Fri Apr 15, 2005 10:07 am
by pickle
The link problem is because of the link code you're outputing
melindaSA wrote:

Code: Select all

echo "<td valign="top" width="80"><font face="Verdana" size="1">
<a href="upload/".$company.".doc" target="_blank">Yes</a></font><br></td>\n";
That's going to give you a link that looks like:

Code: Select all

&lt;a href = &quote;upload/companyx.doc&quote; target = &quote;_blank&quote;&gt;
You need to make the link go to '../upload/companyx.doc' instead

Also, your code will only show a link to a PDF if it exists and if there is no DOC file. You've got an if/elseif clause which causes only one of the links to be output - is that what you wanted? If not, you're gonna need 3 if statements, setting a flag in the first 2 (those which output links to the DOC and PDF files) which denotes that a file has been found, and having the 3rd be conditional on that flag.

Posted: Fri Apr 15, 2005 10:49 am
by melindaSA
Still does not work--showing that there is no file, but there is!

This is what I am using:

Code: Select all

<?php
if(file_exists("../upload/".$company.".doc"))
{
echo "<td valign=\"top\" width=\"80\"><font face=\"Verdana\" size=\"1\"><a href=\"../upload/\".$company.\".doc\" target=\"_blank\">Yes</a></font><br></td>\n";
} 
elseif(file_exists("../upload/".$company.".pdf")) 
{
echo "<td valign=\"top\" width=\"80\"><font face=\"Verdana\" size=\"1\"><a href=\"../upload/\".$company.\".pdf\" target=\"_blank\">Yes</a></font><br></td>\n";
} 
else 
{
echo "<td valign=\"top\" width=\"80\"><font face=\"Verdana\" color=\"white\" size=\"1\">No</font><br></td>\n";
}
?>


I do only want to show either .pdf or .doc, not both, as the user will only upload either a .pdf or a .doc.

Posted: Fri Apr 15, 2005 10:57 am
by pickle
Hmm... Are you SURE the file is there? At this point, I'd write a test page with similar logic, just to make sure.

Code: Select all

if(file_exists("hard_coded_file_path"))
{
  echo "File exists";
}
Also - and this is a style issue - why don't you use single quotes inside your echo statements? I always find it difficult to decipher a statement when there's escape slashes all over.

Posted: Fri Apr 15, 2005 11:46 am
by melindaSA
Yes, the "file exists" did the test.....

Will make the style change next time!

Thanks for the help, but I have no clue why it will not work!

Posted: Fri Apr 15, 2005 11:52 am
by pickle
Try taking out the else's: Have just two if statements - that might help you find what's failing.

Posted: Fri Apr 15, 2005 12:05 pm
by melindaSA
It is working......was my ../upload/, should have been upload/ Thank you so much for your help!!

Code: Select all

<?php
if(file_exists("upload/".$company.".doc"))
	{
	echo "<td valign='top' width='80'><font face='Verdana' size='1'><a href='upload/'.$company.'.doc' target='_blank'>Yes</a></font><br></td>\n";
	} 
	elseif(file_exists("upload/".$company.".pdf")) 
	{
	echo "<td valign='top' width='80'><font face='Verdana' size='1'><a href='upload/'.$company.'.pdf' target='_blank'>Yes</a></font><br></td>\n";
	} 
	else 
	{
	echo "<td valign='top' width='80'><font face='Verdana' color='white' size='1'>No</font><br></td>\n";
	}
?>
[/quote]

Posted: Fri Apr 15, 2005 12:15 pm
by pickle
Good to hear :)