简体   繁体   中英

Use variable from one php page in another via href

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.

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/>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM