Forums

Show link to file only if it has been uploaded

sarkomand 03 May, 2013
Hi.

I hope somebody can help me with this.

I have the following code I can't get to work in the body of a connection. What I'm trying to achieve is create a link to a file only if a file has been uploaded via a Chrono Form, and to show something else if there's no file.

<tr>
<td>
<?php
if
(file_exists('components/com_chronoforms/uploads/CondAdminForm/' . $row['letter']))
{
echo '<a href="components/com_chronoforms/uploads/CondAdminForm/' . $row['letter'] . '">letter</a>';
} else {
echo "no file";
}
?>
</td>
</tr>


If there's a file it does create the correct link (components/com_chronoforms/uploads/CondAdminForm/XYZ.jpg), but if there's no file, instead of showing "no file" the link is incomplete, with no file at the end (components/com_chronoforms/uploads/CondAdminForm/)

What's wrong?

Thanks!
GreyHead 03 May, 2013
Hi sarkomand,

I would make the file urls absolute. Avoid relative URLs in Joomla as the same content can be loaded from different folders.
<tr>
<td>
<?php
if (file_exists(JPATH_SITE.'/components/com_chronoforms/uploads/CondAdminForm/'.$row['letter'])) {
  echo "<a href='".JURI::root()."components/com_chronoforms/uploads/CondAdminForm/{$row['letter']}' >letter</a>";
} else {
  echo "no file";
}
?>
</td>
</tr>

Bob

Bob
sarkomand 03 May, 2013
Thanks, Bob.

Unfortunatelly I get the same result. It seems like the "file_exists" thing is checking for any file in any row and not for a file in that specific row.

So, If I do
<?php
if
(file_exists(JPATH_SITE.'/components/com_chronoforms/uploads/CondAdminForm/' . $row['letter']))
{
echo "yes file";
} else {
echo "no file";
}
?>


I get "yes file" even in the rows of the entries with no file uploaded.
Max_admin 06 May, 2013
Hi,

Maybe you get yes in rows with no files because when the file name is empty, then it checks the folder path, which always exists, you should add this to the if statement:

AND !empty($row['letter'])


Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
sarkomand 06 May, 2013
It worked🙂

Thanks, Max.
This topic is locked and no more replies can be posted.