0

Hey guys,

Hopefully someone out there can help me.

I've written a script that displays the contents of a directory. It also links to the file so when I click on it it displays the file contents.

What I want to do is when I click on that file it creates that a new page with a header and footer include with the contents in the middle.

So far this is the code I have:

<?php
	
	for ($j=65; $j<=90; $j++){
	echo '<div class="list">';
		$a=chr($j);
		if ($a != "J" && $a != "Q" && $a != "X" && $a != "Z") {
		echo '<div class="large">'.$a.'</div>';
		echo '<div class="small">';
		if ($handle = opendir('poems/')) {
			while (false !== ($file = readdir($handle))) {
				if ($file != "." && $file != ".." && $file != "Q" && $file != "J" && $file != "X" && $file != "Z" && $file[0] == $a) {
					echo '<div class="test">';
					echo "<a href=\"poems/$file\" class='nav'>$file</a><br />";
					echo '</div>';
					}
				}
			}
		echo '</div>';
		closedir($handle);
		}
		echo '</div>';
		}
?>

Any help would be amazing! Thanks!

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by peck3277
0

In your original code - just make this small change:

........(rest of code)...........
        if ($file != "." && $file != ".." && $file != "Q" && $file != "J" && $file != "X" && $file != "Z" && $file[0] == $a) {
          //urlencode the filename and call it $uFile...
          $uFile = urlencode($file);
          //make a salted hash of the $uFile - change mysaltstring to something else
          $conf = md5('mysaltstring' . $uFile);
          echo '<div class="test">';
          echo "<a href=\"poems/fileviewer.php?f=$uFile&c=$conf\" class='nav'>$file</a><br />";
          echo '</div>';
        }
      }
    }
    echo '</div>';
    closedir($handle);
  }
  echo '</div>';
}

Just have a php page called fileviewer.php

<?php
//check that the confirm hash is valid for the filename - a simple check against somebody messing with the querystring
if(isset($_GET['c']) && isset($_GET['f']) && $_GET['c'] == md5('mysaltstring' . $_GET['f']) && file_exists(urldecode($_GET['f']))){
//the checks are passed
   $file = urldecode($_GET['f']); //you could also 'sanitize' this step
   $output = file_get_contents($file);
}else{
  $output = "<p>No such file found</p>\n<p><a href="findex.php">Return to file index</a></p>";
}
?> 

...DTD...
...head...
<body>
...your header html...
<div id="fileoutput">
<?php
   echo $output;
?>
</div>
...your footer html...
</body>
</html>

Note - this is off the top of my head and is not tested. It should work, but if not - it's not far off.

BTW: A nicer way to do this would be to implement ajax to reload a div element in a page with a list of available files alongside.

Another thing you could do is send the link to the same page so that the links show up with the content.

There are quite a few ways you could go with this.

Edited by diafol: n/a

0

I might look into using ajax alright. I haven't used it before but I have read a lot of good things about it.

The code you gave me works brilliantly I just have 2 little problems that I don't know how to correct.

The first one is that some of the files im using have spaces in them. the code works for single named files, but not files with spaces in them. Is there any way to get past this?

And the second is some of the file names have special characters like í. Is there a way I can get around this?

If I could achieve the above it would be great but if not I could rename the files but when displaying the names I would display the first line of the file as this has also got the title. I would prefer to do the above but if I couldn't how would i go about doing things this way?

Thanks a million man!

1

The spaces and special characters are a problem, and they tend to cause different problems if on a windows or linux server. If these are your files, simply rename them. If filenames are supplied by users, perhaps you could can a validation routine to "disallow" spaces and accented characters. Force all filenames to be alphanumeric.

You could use a renaming function to replace all spaces with an underscore.

However, this topic was discussed recently here:

http://www.daniweb.com/forums/thread232363.html

and I believe the thread-starter found a solution

//EDIT

Sorry, just re-read your post. With regard to title. Am I correct in assuming the filename is the title? If so and you use underscores in filenames (instead of spaces) - just use:

$rawtitle =  replace_str('_',' ',$file);
//If you don't need the file extension for the title:
$title = substr($rawtitle,0, strpos($rawtitle,'.'));
//I can't remember if the third parameter needs to be +1, -1 or if it's ok as is - always gets me that one!

The $title can be forced to uppercase or lowercase with strtoupper() or strtolower() respectively.

Alternatively, you could use CSS to style the title:

h3.filetitle {text-transform:uppercase} 
/*OR*/
h3.filetitle {text-transform:capitalize}
/*OR*/
h3.filetitle {text-transform:lowercase}

The 'capitalize' will give "A Title With Capitals For All Words"
But you probably knew that.

Edited by diafol: n/a

Votes + Comments
Brilliant
0

Yes you were right about the file name being the title. Thanks for all the help. Everything works as it should now!

What I eneded up doing was renaming my files with no spaces or special characters and because there are title was also the first line of of the file I just put that in an array and then displayed that instead of the file name itself!

Thanks again!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.