I will try to explain this the best I can:
I have an upload page. The user pastes or drags/drops a code file, and uploads it to their user directory. Before they upload, they are required to pick what Language it is via a dropdown menu. That choice value is prepended to the file, and a random string is added. So, if they paste some Java, and click upload, then the file would be
java-iVW827pVjEEo.txt
This part works great.
I have another page, show_file.php, which displays the contents of that file in the same nicely formatted and syntax highlighted fashion. BUT if only works if I manually type in the file name. The problem arises on the user's profile page. The files display on the user's profile like this (I'm changing to titles later. Just need to get this part to work):
That is done via this foreach loop:
foreach($files as $file) {
echo '<a href="'.$link.'">'.basename($file).'</a><br/>';
}
This is how it displays on the show_file.php page:
The syntax highlighting is determined by the first part of the file name. Whatever language is in the string, it will prepend that to the Prism.js class name:
$user_file = "java-NGEw5X8s0W2Q.txt"; //<-- Have to enter it manually right now
$file = fopen("users/$username/code_uploads/$user_file", "r");
switch(true) {
case strpos((string)$file, 'markup'):
$language = 'markup';
break;
case strpos((string)$file, 'java'):
$language = 'java';
break;
//18 other cases follow
And finally, here is the code to actually display the div. It gets the contents of the file, then appends the $language variable to the class as shown here:
<div class="container">
<div class="show-code">
<script src="lib/prism.js"></script> <!-- Load prism.js library -->
<pre><code class="language-<?php echo $language?>"><?php while(! feof($file)) {echo fgets($file);}?></code></pre>
</div>
</div>
I honestly don't know how to link the file path on the user's profile page to the file path that is used on show_file.php. I tried assigning the $file variable to $_SESSION['file'], and then using that on the show_file page, but I get an "Undefined Index" error.
I tried editing my for loop and anchor tag like so
foreach($files as $file) {
//echo '<a href="'.$link.'">'.basename($file).'</a><br/>';
echo '<a href="show_file.php?id=<?php echo $file->id;?>">'.basename($file).'</a><br/>';
}
and assigning my $user_file variable with
$user_file = $_GET['id'],
but now I get
Warning: fopen(users/testUser/code_uploads/<?php echo $file->id;?>): failed to open stream: No such file or directory
What can I do to link my anchor tags to the respective files?
Thanks.

