I've built a blog where the user can attach a PDF to a post. This all works fine apart from one small minor detail.

I call the details from the MYSQL database on the home page and use a while loop to go through the results.

In the DB there is a filename field that saves the path to the DB when the file is uploaded.

When I loop out the results I check to see if the file exists and if it does then it echoes out a link to the file. This bit works fine.

The problem I'm having is when I echo out a PDF icon next to the link. The icon shows up on all of my results even though the link doesn't.

I've tried googling etc and its actually beaten me. Any ideas?

Many thanks in advance.

(Code attached and below)

<?php

while($row = mysql_fetch_array($result)) {
	
    $date = date("l d F Y", $row['timestamp']); //Displays the date in a decent format.
	$time = date("G:i", $row['timestamp']); //Displays the time in 24hr
	$title = stripslashes($row['title']); //Gets rid of slashes that we added before for the DB
    $entry = stripslashes($row['entry']);
	$id = $row['id'];
	$filename = $row['filename'];
	$get_categories = mysql_query("SELECT * FROM php_blog_categories WHERE category_id = $row[category]");
	$category = mysql_fetch_array($get_categories);
	?>

    <p><h2 class="blogTitle"><?php echo $title; ?></h2>

    <span class="blogEntry"><?php echo $entry; ?></span>


	<p>	
<?php
	$fileURL = 'admin/uploads/pdf/'. $filename;

	if (file_exists($fileURL)) { //If there is a value in the fileName column in the DB
		
		//**************************************************************************************************
		//THE NEXT LINE ID THE ONE THAT I'M HAVING THE TROUBLE WITH
		//**************************************************************************************************
		echo "<img src=\"img/icons/PDF-Icon.png\">";
		echo "<small><a href=\"" . $fileURL . "\" target=\"_blank\">" . $filename . "</a></small>";
		//echo out the icon and a link to it.
	} 
	
}
?>

Recommended Answers

All 2 Replies

Member Avatar for diafol

If $filename is EMPTY/BLANK, the directory still stands as being valid, because file_exists returns true for files AND directories.

You could use the is_file() in conjunction with it:

if(file_exists($pathfile) && is_file($pathfile)){

Else you could give the filename a default 'rubbish' name and only overwrite if the DB filename is not "".

I'm sure there's a better way, but those immediately spring to mind.

Spot on. Thank you so m uch

If $filename is EMPTY/BLANK, the directory still stands as being valid, because file_exists returns true for files AND directories.

You could use the is_file() in conjunction with it:

if(file_exists($pathfile) && is_file($pathfile)){

Else you could give the filename a default 'rubbish' name and only overwrite if the DB filename is not "".

I'm sure there's a better way, but those immediately spring to mind.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.