0

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):

screenshot of links

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:

displaying formatted code

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.

1 Answer 1

2

See if this works :)

foreach ($files as $file) {
    //echo '<a href="'.$link.'">'.basename($file).'</a><br/>';
    echo '<a href="show_file.php?id=' . $file->id . '">' . basename($file) . '</a><br/>';
}

Looks like the php tags being added in the echo statement is giving the issue. Let me know!

UPDATE:

Okay, so the original error is gone but there is a problem elsewhere. It's hard to make out what is going on here from just snippets. But, if you are using $_GET to pass through a file name ('id'), this should pick it up on the page it is sent to:

$user_file = $_GET['id']; // There was a comma here previously for some reason instead of semi-colon. Assuming this is an array. If not you can just echo the contents.

print_r($user_file); // This will show you if you have actually received the data. If you have:

foreach ($user_file as $file) {
    echo '<a href="show_file.php?id=' . $file . '">' . basename($file) . '</a><br/>';
}
Sign up to request clarification or add additional context in comments.

6 Comments

I'm sorry, but that did not work. I get fopen(users/testUser/code_uploads/$file->id): failed to open stream: No such file or directory
Hey, I understand. It's very late where I am. Sadly, with that I get Notice: Trying to get property of non-object <file path here> for every file. The links do take me to the page without crashing, but the div displays nothing. I'm reading about $_GET parameters/requests to try and figure it out as well. First time I've ever dealt with them.
It prints users/testUser/code_uploads/c-b7sKqdDUPOVa.txt
WAIT! I understand what is happening now!
When I printed my $_GET variable with "print_r($user_file)" it showed me the whole path "/users/testUser/code_uploads/fileName.txt". My fopen function was trying to find "/users/testUser/code_uploads/fileName.txt/users/testUser/code_uploads/fileName.txt". OF COURSE it wasn't going to find it! My fopen parameter only needed $user_file since it contained the entire path. As soon as I removed the path from the fopen and just left $user_dir it worked! I'm going to accept your answer because your suggestion to use print_r helped me see what was happening, thus solving my problem. :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.