3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by almostbob
0

Sorry to say, but the code you have won't work no matter what you do. PHP is a server-side language and can't interact with client-side easily.

You will need to use the _GET method. You define the file you want in the url and then php can read it from there.
Ex.

<a href="yourpage.php?file=a.txt">File 1</a>

and on the php side you need to check if the file is in the url, then if it is make sure the file is a valid one. After that you open it up and display the contents.

if ( isset( $_GET['file'] ) ) { //?file=a.txt is what this refers to.
  $file = $_GET['file'];
  if ( file_exists( $file ) ) {
    $fh = fopen( $file,'r' );
    $data = fread( $fh,filesize( $file ) );
    fclose( $fh );
    echo "<h1>" . $file . "</h1>";
    echo $data;
    //or if you have php5: echo file_get_contents( $file );
  }
  else {
    die('Invalid File');
  }
}
0
<a href='<?php echo $_SERVER['PHP_SELF']; ?>?x=a.txt'> a </a>
<a href='<?php echo $_SERVER['PHP_SELF']; ?>?x= b.txt'> b </a>
<?php  //hate short codes
$xh = fopen($x, 'r');
$theData = fread($xh, filesize($x));
fclose($xh);
echo '<h1>$x</h1>';
echo $theData;
?>

Edited by almostbob: n/a

This topic has been dead for over six months. 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.