if(file_exists()) [SOLVED]

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
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

if(file_exists()) [SOLVED]

Post 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!
Last edited by melindaSA on Fri Apr 15, 2005 12:10 pm, edited 1 time in total.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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";
}
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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 />";
}
?>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

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

?>
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post 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!
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post 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 />";
   }
}
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post 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!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Try taking out the else's: Have just two if statements - that might help you find what's failing.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post 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]
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Good to hear :)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply